Why the name of a test method may influence other tests?
I have a suite with 2 classes of tests, and when I change a method name of class1, my test in class2 is ok (green).
I noticed that both classes have a method with the same name, but the test that is failing is neither of these. However if I rename any of them, all tests are ok.
Is it okay to have 2 methods with the same name in different classes, but in the same suite? And the fact that another test fails randomly is just a coincidence?
ps: the order of tests runned is changed after I rename that method. ps2: sorry for my bad English.
That picture can explain better my question:
As shown above, we can use the getMethodName method of class TestName to display the name of the test.
When writing unit tests with JUnit, there will likely be situations when tests fail. One possibility is that our code does not meet its test criteria. That means one or more test cases fail due to assertions not being fulfilled.
fail() method It can be used to verify that an actual exception is thrown. Usually based on some input when test case expects an exception at a certain line, providing fail() below that line will verify that exception was not thrown as code execution reached fail() method line.
Rerun Failed Tests with JUnit 4 ExampleWe need two classes, one of them is our Rule Class's RetryRule and the other is our Test Class's RetryRuleTest. In RetryRuleTest class, I will open www.swtestacademy.com and get its title and check it with the WRONG expected title.
There is no bug in JUnit! Our team experienced similar results, which are caused by inproper resource management. You can try to rename your failing test so they are executed first. They should turn green now, that's mostly a sign that a resource is accidently shared between tests. In that case you can try to free the resource in the tear down (@After
).
Here is a little checklist to find the cause:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With