Java – JUnit testcase object instantiation

Is a new (or different) instance of the testcase object used to run each test method in a JUnit test case? Or is an instance reused for all tests?

public class MyTest extends TestCase {
  public void testSomething() { ... }
  public void testSomethingElse() { ... }
}

What is the number of instances of the mytest class when running this test?

If possible, provide a link to the document or source code so that I can verify the behavior

Solution

I can't find a clear answer to your question in the JUnit document, but asjanb wrote that the intention is that each test is independent of other tests, so you can create a new testcase instance for each test to run

If you want to share expensive test settings ("fixtures") in all test cases in the test class, you can use the @ beforeclass annotation on the static method to achieve this result: http://junit.sourceforge.net/javadoc_40/org/junit/BeforeClass.html. Note that new instances can still be created for each test, but this does not affect the static data initialized by the @ beforetest method

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>