是否可以对 JUnit 中的每个测试用例使用不同的 @Before @After?

2024-05-12

我是新来的Java & JUnit并遇到了不同的Fixtures。我在网上搜索了很多,但没有得到答案。

是否可以使用不同的@Before @After对于不同的测试用例JUnit? 例如:我有以下 TC 那么是否可以使用不同的@Before用于测试和不同@Before对于测试1

 import static org.junit.Assert.assertEquals;

 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 testJUnit {

@BeforeClass
public static void setUpBeforeClass() throws Exception {
    System.out.println("Before Class");
}

@AfterClass
public static void tearDownAfterClass() throws Exception {
    System.out.println("Tear Down After Class");
}

@Before
public void setUp() throws Exception {
    System.out.println("Setup");
}

@After
public void tearDown() throws Exception {
    System.out.println("Tear Down");
}

@Test
public void test() {
    int a = 1;
    assertEquals("Testing...", 1, a);
}

@Ignore
@Test
public void test1() {
    int a = 156;
    assertEquals("Testing...", 156, a);
}

}

有人可以指导我吗?


如果你想要不一样的Before and After,将每个测试放在适当的公共类中

被@Before标记的方法会在执行前执行 课堂上的每一次考试。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

是否可以对 JUnit 中的每个测试用例使用不同的 @Before @After? 的相关文章

随机推荐