Is it OK to have multiple assertions in a unit test when testing complex behavior?

后端 未结 3 1743
栀梦
栀梦 2021-01-17 04:06

Here is my specific scenario.

I have a class QueryQueue that wraps the QueryTask class within the ArcGIS API for Flex. This enables me to e

3条回答
  •  时光取名叫无心
    2021-01-17 04:41

    In my opinion, and there will probably be many, there are a couple of things here:

    1. If you must test so many things for one method, then it could mean your code might be doing too much in one single method (Single Responsibility Principle)
    2. If you disagree with the above, then the next thing I would say is that what you are describing is more of an integration/acceptance test. Which allows for multiple asserts, and you have no problems there. But, keep in mind that this might need to be relegated to a separate section of tests if you are doing automated tests (safe versus unsafe tests)
    3. And/Or, yes, the preferred method is to test each piece separately as that is what a unit test is. The closest thing I can suggest, and this is about your tolerance for writing code just to have perfect tests...Is to check an object against an object (so you would do one assert that essentially tests this all in one). However, the argument against this is that, yes it passes the one assert per test test, but you still lose expressiveness.

    Ultimately, your goal should be to strive towards the ideal (one assert per unit test) by focusing on the SOLID principles, but ultimately you do need to get things done or else there is no real point in writing software (my opinion at least :)).

提交回复
热议问题