基于Java的软件测试(下)
来源:互联网 发布:关于网络女主播电影 编辑:程序博客网 时间:2024/06/05 20:08
转载:https://www.douban.com/note/573887591/?type=rec
基于Java的软件测试(下)
在本文的上篇中,我们了解了测试的基本概念,学习了JUnit3和4的基本使用方法,在下篇中,我将介绍另一个测试工具TestNG,并讲一些实际的项目测试中的技巧及方法。
[b] TestNG[/b]
TestNG在设计之初,解决了很多JUnit3中的不足之处;但是从JUnit4开始,TestNG与JUnit的功能越来越接近。总的来讲,TestNG在功能的细节方面考虑得更为周全,因此可根据项目的具体需要来选择使用TestNG或是JUnit。
接下来我们可以继续用javaProj中试用TestNG,在项目的pom.xml中添加TestNG模块:
[code="xml"]
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.1.1</version>
<scope>test</scope>
</dependency>
[/code]
测试代码如下:
[code="java"]
package javaProj;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class TestWithNG {
@BeforeMethod
public void setup() {
// 相当于JUnit的Before
System.out.println("unit test start");
}
@AfterMethod
public void cleanup() {
// 相当于JUnit的After
System.out.println("unit test complete");
}
@BeforeClass
public void init() {
// @BeforeClass标记的方法会在测试初始化时执行一次
System.out.println("Test class initialized.");
}
@AfterClass
public void destroy() {
// @AfterClass标记的方法会在测试全部结束后时执行
System.out.println("Test class destroyed.");
}
@Test
public void test1() {
Assert.assertEquals(1 + 1, 2);
}
// dependsOnMethods用于指定测试间的依赖关系。
// 在这里,test2依赖于test1,只有test1测试
// 通过了,才会进行test2的测试。
@Test(dependsOnMethods = { "test1" })
public void test2() {
Assert.assertEquals(2 + 2, 4);
}
}
[/code]
为了在eclipse中执行这个测试,我们需要安装testng在eclipse中的插件,在MarketPlace中搜索testng:
然后根据提示进行安装,安装完成后重启eclipse便可执行测试代码:
测试结果如下:
接下来我们继续学习TestNG的其它功能:
[b] 参数化的测试[/b]
在测试当中,有时我们需要在测试中使用一些参数,可以使用TestNG的@DataProvider将数据从测试中独立出来,下面是例子:
[code="java"]
package javaProj;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class NGTestParam {
@DataProvider(name ="colors")
public Object[][] colorList() {
return new Object[][] {
{1, "Red"},
{2, "Purple"},
{3, "Green"}
};
}
@Test(dataProvider = "colors")
public void getColors(int i, String color) {
System.out.println("#" + i + ": " + color);
}
}
[/code]
执行结果如下:
此外,TestNG会为我们生成测试报告,此时查看工程目录,应该可以看到TestNG生成的报告:
在这个目录中,有一个index.html,打开它,可以看到报告:
[b] 小结[/b]
通过以上学习,我们熟悉了测试TestNG的使用方法。我将本系列本章中使用到的代码放在了github上面,有兴趣可以迁出:
[code="xml"]
https://github.com/liweinan/unit-test-demo
[/code]
[b] 参考资料[/b]
[i]JUnit Best Practices, Part 4 -- Don't Be Too Assertive[/i] - [url]http://www.testoften.com/2007/02/junit-best-practices-part-4-dont-be-too.html[/url]
[i]JUnit 4 Vs TestNG – Comparison[/i] - [url]http://www.mkyong.com/unittest/junit-4-vs-testng-comparison/[/url]
[i]TestNG Maven plug-ins[/i] - [url]http://testng.org/doc/maven.html[/url]
[i]Using TestNG[/i] - [url]http://maven.apache.org/plugins/maven-surefire-plugin/examples/testng.html[/url]
[i]JUnit 4 Tutorial 6 – Parameterized Test[/i] - [url]http://www.mkyong.com/unittest/junit-4-tutorial-6-parameterized-test/[/url]
[i]TestNG Eclipse plug-in[/i] - [url]http://testng.org/doc/eclipse.html[/url]
[i]TestNG Tutorial 6 – Parameterized Test[/i] - [url]http://www.mkyong.com/unittest/testng-tutorial-6-parameterized-test/[/url]
0 0
- 基于Java的软件测试(下)
- 基于Java的软件测试(上)
- 基于生命周期的软件测试
- 【软件测试】基于应用服务器的测试
- 软件测试的分类(下)
- 软件测试基础与基于生命周期的软件测试
- 基于协作的互联网软件测试系统
- 基于软件度量的测试体系建设
- 第三章基于生命周期的软件测试
- 基于金融行业的软件测试分析
- jCpuTest 小小的 基于JAVA的 系统性能测试软件 首发啦~~
- 基于Web的系统测试与传统的软件测试
- 基于Web的系统测试与传统的软件测试
- 基于产品的测试管理(用友软件测试流程初探)
- 基于python的互联网软件测试开发(自动化测试)-全集合
- 基于python的互联网软件测试开发(自动化测试)-全集合
- 基于python的互联网软件测试开发(自动化测试)-全集合
- 基于python的互联网软件测试开发(自动化测试)-全集合
- Linux crontab 命令
- Unity自定义消息弹出框
- LeetCode | Remove Element
- 解决Eclipse开启后闪退问题方案
- [Python] 'unicode' object is not callable
- 基于Java的软件测试(下)
- LeetCode Ransom Note(字符串)
- sudo: /usr/bin/sudo must be owned by uid 0 and have the setuid bit set的解决办法
- 使用HorizontalScrollView 注意事项
- 单例模式以及双检锁DCL
- Linux电源管理(11)_Runtime PM之功能描述
- Android Studio的格式化快捷键ctrl+shift+F失效?
- Hdu 5861 Road
- MessageBox