junit tearDownClass() vs tearDown()
What is 开发者_如何学Pythondifference between tearDownClass() & tearDown() methods?
Where can I find documentation for both.
junit.org documentation of JUnit listed only tearDown() not tearDownClass(): http://www.junit.org/apidocs/junit/framework/TestCase.html#setUp()
Use the API's tearDownAfterClass() and tearDown() with the Annotations @AfterClass and @After. The code within tearDownAfterClass() would be executed only once after all the unit tests written in Junit have been executed. Clean up code can be written here to release the resources after all the tests have been executed. The code within tearDown() would be executed after executing each of the test scenarios.
These API's are part of JUnit 4.
Below is a sample code to understand the invocation of these API's:
public class TestJUnit {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
System.out.println("Executing a JUNIT test file");
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
System.out.println("Execution of JUNIT test file done");
}
@Before
public void setUp() throws Exception {
System.out.println("Executing a new test");
}
@After
public void tearDown() throws Exception {
System.out.println("Execution done");
}
@Test
public void test1() {
System.out.println("test1 ...");
}
@Test
public void test2(){
System.out.println("test2 ...");
}
}
Output: Executing a JUNIT test file Executing a new test test1 Execution done Executing a new test test2 Execution done Execution of JUNIT test file done
The API's setUpBeforeClass() and setUp() with the Annotations @BeforeClass and @Before respectively would behave as follows:
setUpBeforeClass - Useful to have initialization code here. Code written in this method would get executed only once and execution would happen prior to execution of the individual tests.
setUp() - Code within this block would get executed prior to each of the individual tests.
There's a AfterClass annotation in the JUnit 4.x API, is that what you meant?
tearDown
occurs after executing each test method of the TestCase. There is a separate hook (the AfterClass I linked to) that executes after all the test methods of the TestCase have run.
I don't think the 3.x Junit API had any concept of an after-class teardown. Maybe you're thinking of TestNG?
From what I've seen the Java unittesting seems to match Python pretty closely, so if JUnit is the same as the Python testcases I'm working with then in a testcase class setUp()
and tearDown()
are called before and after every test()
you write.
setUpClass()
and tearDownClass()
are called once at the beginning and end of a particular testcase class.
So to illustrate this in what I'm doing I have in Python:
class exampleUnitTest(SeleniumTestCase):
def setUp(self):
# setup each test
def test1(self):
# run test process
def test2(self):
# run test process
def tearDown(self):
# teardown each test
@classmethod
def tearDownClass(cls):
# teardown at end of all tests
To understand @After & @AfterClass I want to provide this code sample:
public class ExampleTest {
private static final Logger LOG = LoggerFactory.getLogger(ExampleTest.class);
/**
* This method won't be executed if the unit test will be (externally) terminated (e.g. from within the IDE).
* You have to act **really** fast in this case.
*/
@BeforeClass
public static void beforeClass() throws Exception {
LOG.info("@BeforeClass"); // will be always executed, even if a test fails throwing any Exception
}
/**
* This method won't be executed if the unit test will be (externally) terminated (e.g. from within the IDE).
*/
@AfterClass
public static void afterClass() throws Exception {
// executed after the class has been initialized, only once
LOG.info("@AfterClass"); // will be always executed, even if a test fails throwing any Exception
}
/**
* This method won't be executed if the unit test will be (externally) terminated (e.g. from within the IDE).
* You have to act *really* fast in this case.
*/
@Before
public void setUp() throws Exception {
LOG.info("@Before"); // will be always executed, even if a test fails throwing any Exception
}
/**
* This method won't be executed if the unit test will be (externally) terminated (e.g. from within the IDE).
*/
@After
public void tearDown() throws Exception {
// executed for every single unit test (annotated with @Test) within this test class
LOG.info("@After"); // will be always executed, even if a test fails throwing any Exception
}
@Test
public void someTest() throws Exception {
Thread.sleep(10000); // interrupts current "main" Thread in the "main" thread group
throw new Error("VM Error"); // allowed as Exception is part of this test method's signature ("throws" clause)
}
}
精彩评论