Answer the question
In order to leave comments, you need to log in
Should unit tests always be independent of each other?
Most unit testing evangelists argue that unit tests should be independent of each other. Why?
It seems to me that this statement is not entirely true and here's why: Let's
assume that when developing the a_Car class, I use an_Engine as another class. There is a relationship between the whole and the part. As a conclusion, the full performance of the a_Car class depends on the correct operation of the an_Engine class. In other words, if the process of testing the an_Engine class shows that it has 45% crashes from the entire set of tests for an_Engine, then this means that the a_Car class should not be used, because. it doesn't work correctly!
But most evangelicals, despite this obvious situation, continue to argue that tests should be independent, why?
Really in life they will try to move off and start a car whose engine is 45% off?
In my opinion, it is much more correct to say that: "The car class does not work correctly due to the incorrect operation of the Engine class for which 45% of crashes were detected" is a more honest statement that justifies the stop of the Car class testing process.
What do I still not understand? Please explain to experienced unit testers who use unit testing for more than one month, and preferably more than one year.
Answer the question
In order to leave comments, you need to log in
Since there is bdd in the tags, then you already know what is Given and what is When.
So, two tests are dependent if the start of the second test is not possible without first executing the first test (correct execution). So at the beginning, many develop. Test1 creates an object named objectName123, and Test2 looks up objectName123 in Given and performs some action on it in When, plus a dozen checks to see if the action was successful in Then.
And you shouldn't do that, for obvious reasons.
An independent test is arranged like this.
Test1 creates an objectNameTest1_123 works with it, makes a dozen checks in Then.
Test2 in Given calls the same methods that create an object in Test1, creates an objectNameTest2_123 object, but performs one basic check, or does not perform any validation checks at all (only the Given and When part of Test1 is called).
Test2 assumes that Test1 works. And if Test1 fails, then the precondition for starting testing for Test1 is not met (after all, the same methods are called in one case, as the main logic of the test, in the second as preconditions). And the report will show you which tests failed and which didn't even start (because other tests failed).
Independence is that Test2 itself creates the necessary context for itself. Not relying on this context being prepared during the execution of the previous tests (Test1).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question