tearDownClass()& tearDown()方法?
我在哪里可以找到两者的文档。
junit.org文档列出的JUnit只有tearDown()而不是tearDownClass(): http://www.junit.org/apidocs/junit/framework/TestCase.html#setUp()
答案 0 :(得分:4)
使用API的tearDownAfterClass()和tearDown()以及注释@AfterClass和@After。 在执行了在Junit中编写的所有单元测试之后,tearDownAfterClass()中的代码只会执行一次。在执行完所有测试后,可以在此处编写清理代码以释放资源。 tearDown()中的代码将在执行每个测试场景后执行。
这些API是JUnit 4的一部分。
以下是了解这些API调用的示例代码:
公共类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 ...");
}
}
<强>输出:强> 执行JUNIT测试文件 执行新测试 TEST1 执行完毕 执行新测试 TEST2 执行完毕 完成JUNIT测试文件的执行
API的setUpBeforeClass()和setUp()分别带有Annotations @BeforeClass和@Before,其行为如下:
setUpBeforeClass - 这里有初始化代码很有用。用这种方法编写的代码只会执行一次,执行将在执行各个测试之前执行。
setUp() - 此块中的代码将在每次单独测试之前执行。
答案 1 :(得分:3)
JUnit 4.x API中有一个AfterClass注释,这是你的意思吗?
执行TestCase的每个测试方法后发生 tearDown
。在TestCase的所有测试方法运行之后,都会有一个单独的钩子(我链接到的AfterClass)。
我不认为3.x Junit API有任何关于课后拆解的概念。也许你在考虑TestNG?
答案 2 :(得分:1)
从我看到的Java单元测试看起来非常接近Python,所以如果JUnit与我正在使用的Python测试用例相同,那么在测试用例类setUp()
和tearDown()
中在你写的每个test()
之前和之后调用。
setUpClass()
和tearDownClass()
在特定测试用例类的开头和结尾调用一次。
为了说明我在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
答案 3 :(得分:0)
了解 @After &amp; @AfterClass 我想提供此代码示例:
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)
}
}