流程简写如下:
class setup -> instance setup -> one test case -> instance tear down -> instance setup -> one test case -> instance tear down -> … -> class tear down
直接show code
python
class SoundTest(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
print('set up class')
@classmethod
def tearDownClass(cls) -> None:
print('tear down class')
def setUp(self) -> None:
print('set up')
def tearDown(self) -> None:
print('tear down')
def test_hello1(self):
print('hello1')
def test_hello2(self):
print('hello 2')
输出结果
Launching unittests with arguments python -m unittest sound.SoundTest in D:\code\python\python-demo\test_package\sound
Ran 2 tests in 0.002s
OK
set up class
set up
hello1
tear down
set up
hello 2
tear down
tear down class
Process finished with exit code 0
java
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
public class ExecutionProcedureJunit {
//execute only once, in the starting
@BeforeClass
public static void beforeClass() {
System.out.println("in before class");
}
//execute only once, in the end
@AfterClass
public static void afterClass() {
System.out.println("in after class");
}
//execute for each test, before executing test
@Before
public void before() {
System.out.println("in before");
}
//execute for each test, after executing test
@After
public void after() {
System.out.println("in after");
}
//test case 1
@Test
public void testCase1() {
System.out.println("in test case 1");
}
//test case 2
@Test
public void testCase2() {
System.out.println("in test case 2");
}
}