How many unit tests should I write per function/method?

后端 未结 12 2077
忘了有多久
忘了有多久 2020-12-05 18:36

Do you write one test per function/method, with multiple checks in the test, or a test for each check?

相关标签:
12条回答
  • 2020-12-05 18:56

    I try to separate out Database tests and Business Logic Tests (using BDD as others here recommend), running the Database ones first ensures your Database is in a good state before asking your application to play with it.

    There's a good podcast show with Andy Leonard on what it involves and how to do it, and if you'd like a bit more information, I've written a blog post on the subject (shameless plug ;o)

    0 讨论(0)
  • 2020-12-05 18:57

    I write at least one test per method, and somtimes more if the method requires some different setUp to test the good cases and the bad cases.

    But you should NEVER test more than one method in one unit test. It reduce the amount of work and error in fixing your test in case your API changes.

    0 讨论(0)
  • 2020-12-05 19:02

    I would suggest a test case for every check. The more you keep atomic, the better your results are!

    Keeping multiple checks in a single tests will help you generate report for how much functionality needs to be corrected.

    Keeping atomic test case will show you the overall quality !

    0 讨论(0)
  • 2020-12-05 19:05

    I have a test per capability the function is offering. Each test may have several assertions, however. The name of the testcase indicates the capability being tested.

    Generally, for one function, I have several "sunny day" tests and one or a few "rainy day" scenario, depending of its complexity.

    0 讨论(0)
  • 2020-12-05 19:06

    A test case for each check. It's more granular. It makes it much easier to see what specific test case failed.

    0 讨论(0)
  • 2020-12-05 19:07

    In general one testcase per check. When tests are grouped around a particular function it makes refactoring (eg removing or splitting) that function more difficult because the tests also need a lot of changes. It is much better to write the tests for each type of behaviour that you want from the class. Sometimes when testing a particular behaviour it makes sense to have multiple checks per test case. However, as the tests become more complicated it makes them harder to change when something in the class changes.

    0 讨论(0)
提交回复
热议问题