junit tearDownClass()与tearDown()的比较
tearDownClass()和tearDown()方法之间的区别是什么 在哪里可以找到两者的文档 junit.org的junit文档只列出了tearDown()而不是tearDownClass(): ()在JUnit4.xapi中有一个注释,这就是你的意思吗junit tearDownClass()与tearDown()的比较,junit,Junit,tearDownClass()和tearDown()方法之间的区别是什么 在哪里可以找到两者的文档 junit.org的junit文档只列出了tearDown()而不是tearDownClass(): ()在JUnit4.xapi中有一个注释,这就是你的意思吗 tearDown在执行TestCase的每个测试方法后发生。有一个单独的钩子(链接到的AfterClass)在TestCase的所有测试方法运行之后执行 我不认为3.xJUnitAPI有任何课后拆卸的概念。也许您正在考虑TestNG?从我
tearDown
在执行TestCase的每个测试方法后发生。有一个单独的钩子(链接到的AfterClass)在TestCase的所有测试方法运行之后执行
我不认为3.xJUnitAPI有任何课后拆卸的概念。也许您正在考虑TestNG?从我所看到的情况来看,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
将API的tearDownAfterClass()和tearDown()与注释@AfterClass和@After一起使用。 在所有用Junit编写的单元测试执行完毕后,tearDownAfterClass()中的代码将只执行一次。可以在这里编写清理代码,以便在执行所有测试后释放资源。 tearDown()中的代码将在执行每个测试场景后执行 这些API是JUnit4的一部分 下面是理解这些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测试文件
执行新测试
测试1
执行完毕
执行新测试
测试2
执行完毕
JUNIT测试文件的执行完成
API的setUpBeforeClass()和setUp()分别带有@BeforeClass和@Before注释,其行为如下:
setUpBeforeClass-在这里有初始化代码很有用。用这个方法编写的代码只执行一次,并且在执行单个测试之前执行
setUp()-此块中的代码将在每个单独测试之前执行。要了解@After&@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)
}
}
如果JUnit文档没有引用它,那么它是从哪里来的?您使用的是什么版本的JUnit?可能的重复:JUnit 3没有
tearDownClass()
,但是这就是我所说的用@AfterClass
注释的方法。