1.配置
建立Maven Project
建立pom.xml文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.jerry</groupId> <artifactId>mytest</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>mytest</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.jerry</groupId> <artifactId>mytest</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>mytest</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> |
安装:https://www.cnblogs.com/xusweeter/p/6559196.html,将org.testng.eclipse.updatesite.zip 拷贝到%ECLIPSE_HOME%\dropins,建立TestNG Class。
2. 基本架构
被测代码
package com.jerry; public class Calculator{ private static int result; public void add(int n) { result = result + n; } public void substract(int n) { result = result - n; } public void multiply(int n) { result = result * n; } public void divide(int n){ try { result = result / n; }catch(ArithmeticException ex){ System.out.println(ex); throw new ArithmeticException("The n not allowed to 0!!") } } } |
最基本的测试代码:
package com.jerry.mytest; import org.testng.annotations.Test; import org.testng.AssertJUnit; import org.testng.annotations.BeforeMethod; public class CalculatorTest { private static Calculator calculator = new Calculator(); @Test public void testAdd() { calculator.add(2); calculator.add(3); AssertJUnit.assertEquals(5,calculator.getResult()); } @Test public void testSubstract() { calculator.add(5); calculator.substract(3); AssertJUnit.assertEquals(2,calculator.getResult()); } @Test public void testMultiply() { calculator.add(3); calculator.multiply(2); AssertJUnit.assertEquals(6,calculator.getResult()); } @Test public void testDivide() { calculator.add(9); calculator.divide(3); AssertJUnit.assertEquals(3,calculator.getResult()); } @BeforeMethod public void beforeMethod() { calculator.clear(); } } |
3. TestNG的修饰符
描述装饰符的程序
package MyUnit.MyTestNG; import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.AfterGroups; import org.testng.annotations.AfterMethod; import org.testng.annotations.AfterSuite; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeGroups; import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeSuite; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; public class MyFirstNG { @Test(groups="group1") public void test1() { System.out.println("test1 from group1"); Assert.assertTrue(true); } @Test(groups="group1") public void test11() { System.out.println("test11 from group1"); Assert.assertTrue(true); } @Test(groups="group2") public void test2() { System.out.println("test2 from group2"); Assert.assertTrue(true); } @BeforeTest public void beforeTest() { System.out.println("beforeTest"); } @AfterTest public void afterTest() { System.out.println("afterTest"); } @BeforeClass public void beforeClass() { System.out.println("beforeClass"); } @AfterClass public void afterClass() { System.out.println("afterClass"); } @BeforeSuite public void beforeSuite() { System.out.println("beforeSuite"); } @AfterSuite public void afterSuite() { System.out.println("afterSuite"); } //只对group1有效,即test1和test11 @BeforeGroups(groups="group1") public void beforeGroups() { System.out.println("beforeGroups"); } //只对group1有效,即test1和test11 @AfterGroups(groups="group1") public void afterGroups() { System.out.println("afterGroups"); } @BeforeMethod public void beforeMethod() { System.out.println("beforeMethod"); } @AfterMethod public void afterMethod() { System.out.println("afterMethod"); } } |
运行结果(缩进为了看起来方便,我自己设置的)
beforeSuite beforeTest beforeClass beforeGroups beforeMethod test1 from group1 afterMethod beforeMethod test11 from group1 afterMethod afterGroups beforeMethod test2 from group2 afterMethod afterClass afterTest PASSED: test1 PASSED: test11 PASSED: test2 =============================================== Default test Tests run: 3, Failures: 0, Skips: 0 =============================================== afterSuite =============================================== Default suite Total tests run: 3, Failures: 0, Skips: 0 =============================================== |
4. 异常测试
被测的程序
public void divide(int n){ try { result = result / n; }catch(ArithmeticException ex){ System.out.println(ex); throw new ArithmeticException("The n not allowed to 0!!"); } } |
测试程序
@Test(expectedExceptions = ArithmeticException.class) public void divisionWithException() { calculator.add(9); calculator.divide(0); } |
5. 忽略测试
@Test(enabled=false) public void TestNgLearn1() { System.out.println("this is TestNG test case1"); } |
6.参数化
6.1使用textng.xml传送参数
test.xml
<?xml version="1.0" encoding="UTF-8"?> <suite name="Suite" parallel="false"> <test name="Test"> <parameter name="first" value="3" /> <parameter name="second" value="3"/> <parameter name="result" value="6"/> <classes> <class name="MyUnit.MyTestNG.MyTestNG"/> </classes> </test> <!-- Test --> </suite> <!-- Suite --> |
测试代码
@Test @Parameters({"first","second","result"}) public void TestParemByXML(int first, int second,int result) { calculator.add(first); calculator.add(second); AssertJUnit.assertEquals(result,calculator.getResult()); } |
注意:
1.只能设置一组参数
2.必须运行test.xml,而非测试代码
6.2使用DataProvider传送参数
可以设置多组
@DataProvider(name = "provideNumbers") public Object[][] provideData() { return new Object[][] { { 3, 3, 9 }, { -3, -3, 9 }, { 3, -3, -9 } }; } @Test(dataProvider = "provideNumbers") public void TestParemByDataProvider(int first, int second,int result) { calculator.add(first); calculator.multiply(second); AssertJUnit.assertEquals(result,calculator.getResult()); } |
7.总结
本文出自51Testing会员投稿,51Testing软件测试网及相关内容提供者拥有内容的全部版权,未经明确的书面许可,任何人或单位不得对本网站内容复制、转载或进行镜像,否则将追究法律责任。