Is it bad form to count on the order of your NUnit unit tests

后端 未结 8 1765
别跟我提以往
别跟我提以往 2021-02-13 06:16

I have been creating Unit tests like crazy and find that I\'m often having to set up something in one test that I just tore down in a previous test. Is it ever reasonable to cr

相关标签:
8条回答
  • 2021-02-13 06:37

    Unit tests are intended to stand alone, not be run as a sequential script. If you actually need them run sequentially, collect them into a single test function.

    If your unit tests suffer from expensive set-up, you may be doing integration testing when you think you're doing unit testing. If you're hitting a SQL database inside most of your unit tests, you're actually integration testing with your data access layer.

    0 讨论(0)
  • 2021-02-13 06:43

    I would strongly advise to make all your unit tests independent.

    Your business logic / database structure etc. may change over time, so that you'll eventually have to replace or rewrite (or even discard) existing unit tests - and if you have several other tests depending on the one that you're replacing, this might cause unnecessary troubles because you'd have to go through all of the other tests as well and check if these are still working as expected.

    In addition, one failing unit test should not be able to drag many others (that might perfectly work on their own) down.

    0 讨论(0)
  • 2021-02-13 06:50

    If you have stateful tests (a common problem with database work - which is what I do when I'm not on SO), then it seems to me that avoiding order within a test file is not absolutely necessary. However, you have to recognize that if you have 2 tests, with test 2 depending on test 1 passing, then you will get a 'catastrophic' double failure if test 1 fails, because test 2 doesn't have the expected setup (and, what's more, you want to worry if test 2 does pass after test 1 failed if you think that test 2 depends on test 1 passing).

    That's why you want the tests independent whenever possible - both intra-file and inter-file.

    It would be very unwise to depend on the order between (sets of) tests in different files.

    0 讨论(0)
  • 2021-02-13 06:52

    I really wouldn't rely on ordering of tests. Instead, I'd pull the common setup code into a separate method and call that from both the simple test and the more complicated one. Alternatively, just call the insertion test itself at the start of the deletion test.

    0 讨论(0)
  • 2021-02-13 06:54

    Unfortunately run order of the unit test is not predictable or at least could be changed in future. E.g. unit testing framework will be changed so each test will be executed in separate thread. So from my point of view using test order is not reasonable. On the other hand you can create a set of small independent tests to test small parts of your code and then create one or several large tests that will run your small tests in specific order.

    0 讨论(0)
  • 2021-02-13 06:55

    I would view each test as completely independent from any other test. Even if you could mandate the order of the tests, it would be a maintenance nightmare when the tests must change.

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