JUnit4
1.javadoc: http://junit.org/javadoc/latest/index.html
2.lib:
2.1. https://code.google.com/archive/p/hamcrest/downloads
hamcrest-core-xx.jar and hamcrest-library-xx.jar are for the assertThat function.
2.2. https://github.com/junit-team/junit/wiki/Download-and-Install
http://search.maven.org/#search|gav|1|g:%22junit%22%20AND%20a:%22junit%22
junit-4.12.jar is for junit.
3.assertThat:
int z = new T1().add(5,3);
// assertEquals(8, z);
// assertTrue(z>3);
// // assertTrue(z>10);
// assertTrue("heeeee",z>10);
assertThat(z, is(8));
assertThat(z,allOf(greaterThan(5),lessThan(10)));
/*
assertThat(n,allOf(greaterThan(1),lessThan(15)));
assertThat(n,anyOf(greaterThan(16),lessThan(8)));
assertThat(n,anything());
assertThat(str,is("junit4test"));
assertThat(str,not("junit4test"));
assertThat(str,containsString("junit4test"));
assertThat(str,endsWith("junit4test"));
assertThat(str,startsWith("junit4test"));
assertThat(n,equalTo(nExpected));
assertThat(str,equalToIgnoringCase("junit4testWorks"));
assertThat(str,equalToIgnoringWhiteSpace("junit4testWorks"));
assertThat(d,closeTo(3.0,0.3)); 接近3.0,误差不大于0.3
assertThat(d,greaterThan(3.0));
assertThat(d,lessThan(10.0));
assertThat(d,greaterThanOrEqualTo(5.0));
assertThat(d,lessThanOrEqualTo(16.0));
assertThat(map,hasEntry("junit4test","junit4test"));
assertThat(iterable,hasItem("junit4test"));
assertThat(map,hasKey("junit4test"));
assertThat(map,hasValue("junit4test"));
*/
4.annotation
@Ignore
@Test(expected=java.lang.ArithmeticException.class,timeout=100)
//or @Test
public void testDivide(){
int z = new T1().divide(8, 0);
int a = new T1().divide(2, 1);
assertThat(a ,is(2));
}
@Before
public void before(){
System.out.println("It is before each @Test functon.E.g. read config file");
}
@After
public void after(){
System.out.println("It is after each @Test functon");
}
@BeforeClass
public static void beforeClass(){
System.out.println("It is running before this class be a instance.E.g. open DB");
}
@AfterClass
public static void afterClass(){
System.out.println("It is running After this class be a instance.E.g. close DB");
}
5. Run As->Run Configuration:
option: Run all test in the selected project, package or source folder:
testNG
2.lib:
2.1. https://code.google.com/archive/p/hamcrest/downloads
hamcrest-core-xx.jar and hamcrest-library-xx.jar are for the assertThat function.
2.2. https://github.com/junit-team/junit/wiki/Download-and-Install
http://search.maven.org/#search|gav|1|g:%22junit%22%20AND%20a:%22junit%22
junit-4.12.jar is for junit.
3.assertThat:
int z = new T1().add(5,3);
// assertEquals(8, z);
// assertTrue(z>3);
// // assertTrue(z>10);
// assertTrue("heeeee",z>10);
assertThat(z, is(8));
assertThat(z,allOf(greaterThan(5),lessThan(10)));
/*
assertThat(n,allOf(greaterThan(1),lessThan(15)));
assertThat(n,anyOf(greaterThan(16),lessThan(8)));
assertThat(n,anything());
assertThat(str,is("junit4test"));
assertThat(str,not("junit4test"));
assertThat(str,containsString("junit4test"));
assertThat(str,endsWith("junit4test"));
assertThat(str,startsWith("junit4test"));
assertThat(n,equalTo(nExpected));
assertThat(str,equalToIgnoringCase("junit4testWorks"));
assertThat(str,equalToIgnoringWhiteSpace("junit4testWorks"));
assertThat(d,closeTo(3.0,0.3)); 接近3.0,误差不大于0.3
assertThat(d,greaterThan(3.0));
assertThat(d,lessThan(10.0));
assertThat(d,greaterThanOrEqualTo(5.0));
assertThat(d,lessThanOrEqualTo(16.0));
assertThat(map,hasEntry("junit4test","junit4test"));
assertThat(iterable,hasItem("junit4test"));
assertThat(map,hasKey("junit4test"));
assertThat(map,hasValue("junit4test"));
*/
4.annotation
@Ignore
@Test(expected=java.lang.ArithmeticException.class,timeout=100)
//or @Test
public void testDivide(){
int z = new T1().divide(8, 0);
int a = new T1().divide(2, 1);
assertThat(a ,is(2));
}
@Before
public void before(){
System.out.println("It is before each @Test functon.E.g. read config file");
}
@After
public void after(){
System.out.println("It is after each @Test functon");
}
@BeforeClass
public static void beforeClass(){
System.out.println("It is running before this class be a instance.E.g. open DB");
}
@AfterClass
public static void afterClass(){
System.out.println("It is running After this class be a instance.E.g. close DB");
}
5. Run As->Run Configuration:
option: Run all test in the selected project, package or source folder:
testNG
评论
发表评论