Junit简单使用

简单介绍junit单元测试框架,两种方式:注解,显示调用

1.注解:在JUNIT4中才有,单个单元测试

import static org.junit.Assert.assertEquals;
import junit.framework.TestCase;


import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
public class TestUnit {
	@Test
	public void method(){
		assertEquals("this is a error","123","123");
	}
	@After
	public void afterexecute(){
		System.out.println("after");
	}
	@Before
	public void beforeexecute(){
		System.out.println("before");
	}
}

 

套件测试即测试多个单元

 

i

mport org.junit.runner.RunWith;
import org.junit.runners.Suite.SuiteClasses;
import org.junit.runners.Suite;  
@RunWith(Suite.class) //调用执行测试
@SuiteClasses({TestUnit.class,多个单元class})
public class Testall {
	
	public Testall() {//必须为无参构造函数
		// TODO Auto-generated constructor stub
	}
	
}

 2.代码显示调用

单个测试继承TestCase

public class HelloTest extends TestCase{
	
	public void testcase(){
		this.assertEquals("不rer相等", 13, 12);
	}
	
	public void printsbefore(){
		System.out.println("before");
	}
	
	
	public void printafter(){
		System.out.println("after");
	}
	
}

import static org.junit.Assert.assertEquals;
import junit.framework.TestCase;


import org.junit.After;
import org.junit.Before;
import org.junit.Test;


public class ShellTest extends TestCase{
	
	public void setUps(){
		System.out.println("before");
	}
	
	public void tearDowns(){
		System.out.println("after");
	}
	
	public void testthree(){
		assertEquals("equal error",1323,123);
		
	}
}

 

 

 套件测试

 

import junit.framework.Test;

import junit.framework.TestSuite;

public class AllTests {

	public static Test suite() {//必须是这种规范不可改
		/*TestSuite suite = new TestSuite(AllTests.class.getName());
		//$JUnit-BEGIN$
		suite.addTestSuite(HelloTest.class);
		*/
		
		TestSuite suite=new TestSuite(AllTests.class.getName());
		suite.addTestSuite(ShellTest.class);
		suite.addTestSuite(HelloTest.class);
		
		//$JUnit-END$
		return suite;
	}

}

 

简单使用非常简单,我没有深入了解。

你可能感兴趣的:(JUnit)