当前位置:   article > 正文

JUnit单元测试六大常用 - @Test、@Before、@After、@BeforeClass、@AfterClass、@Ignore_ut@before和@

ut@before和@


Junit注解修饰的方法必须是 public 、void、无参的测试方法

单元测试注解
@BeforeClass:最先运行,且只运行一遍 - 修饰静态方法
@Before:紧随在@Test运行之前,有多少个@Test就运行多少遍 - 同@Test共存亡
@Test:只有含有@Test,其他注解才能作用
@After:紧随在@Test运行之后,有多少个@Test就运行多少遍 - 同@Test共存亡
@AfterClass:所有注解运行完毕,才最后运行,只运行一遍 - 修饰静态方法
@Ignore:
修饰@Test测试方法:junit测试时不运行该@Test方法
修饰@Test测试方法:所有注解方法都不运行

1. 前5大注解测试

public class Test1 {
		
	@BeforeClass
	public static void beforeClazz() {
		System.out.println("beforeClazz" );
	}
			
	@Before
	public void before() {
		System.out.println("before");
	}
	
	@Test
	public void test1() {
		System.out.println("test" + this);
	}
	
	@Test
	public void test2() {
		System.out.println("test2" + this);
	}
	
	@After
	public void after() {
		System.out.println("after");
	}
	
	@AfterClass
	public static void afterClazz() {
		System.out.println("afterClazz");
	}	
	
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33


  运行结果
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MX978MuR-1576579796790)(en-resource://database/13609:0)]

2. @Test

当测试类中没有一个@Test修饰的方法,则单元测试类不可以被启动

3. @Ignore

即使修饰了所有@Test方法,但仍然能启动单元测试类

  修饰所有@Test - 依然能运行@BeforeClass、@AfterClass注解的静态方法

public class Test1 {
	
	@BeforeClass
	public static void beforeClazz() {
		System.out.println("beforeClazz" );
	}
	
	@Before
	public void before() {
		System.out.println("before");
	}
	
	@Test
	@Ignore
	public void test1() {
		System.out.println("test" + this);
	}
	
	@Test
	@Ignore
	public void test2() {
		System.out.println("test2" + this);
	}
	
	@After
	public void after() {
		System.out.println("after");
	}
	
	@AfterClass
	public static void afterClazz() {
		System.out.println("afterClazz");
	}	
	
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35


  运行结果
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lrkiMuyT-1576579796802)(en-resource://database/13611:0)]

  修饰整个测试类,所有测试注解修饰的方法都不能运行

@Ignore
public class Test1 {
	
	@BeforeClass
	public static void beforeClazz() {
		System.out.println("beforeClazz" );
	}
	
	@Before
	public void before() {
		System.out.println("before");
	}
	
	@Test
	public void test1() {
		System.out.println("test" + this);
	}
	
	@Test
	public void test2() {
		System.out.println("test2" + this);
	}
	
	@After
	public void after() {
		System.out.println("after");
	}
	
	@AfterClass
	public static void afterClazz() {
		System.out.println("afterClazz");
	}	
	
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34


  运行结果
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dcOSyzJo-1576579796810)(en-resource://database/13615:0)]

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/653830
推荐阅读
相关标签
  

闽ICP备14008679号