Is Assert.Fail() considered bad practice?

前端 未结 14 1165
面向向阳花
面向向阳花 2021-01-31 07:20

I use Assert.Fail a lot when doing TDD. I\'m usually working on one test at a time but when I get ideas for things I want to implement later I quickly write an empty test where

14条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-31 07:36

    This is our use case for Assert.Fail().

    One important goal for our Unit tests is that they don't touch the database.

    Sometimes mocking doesn't happen properly, or application code is modified and a database call is inadvertently made.

    This can be quite deep in the call stack. The exception may be caught so it won't bubble up, or because the tests are running initially with a database the call will work.

    What we've done is add a config value to the unit test project so that when the database connection is first requested we can call Assert.Fail("Database accessed");

    Assert.Fail() acts globally, even in different libraries. This therefore acts as a catch-all for all of the unit tests.

    If any one of them hits the database in a unit test project then they will fail.

    We therefore fail fast.

提交回复
热议问题