一微网站建设公司,wordpress社区型主题,佛山网站设计资讯,wordpress添加缩略图大约是时候单元测试的开发人员能够使用批注在Parallel中运行测试。 在今天的博客文章中#xff0c;我们将介绍如何使用Easytest提供的注释使传统的Junit测试并行运行。 易测 是在JUnit之上构建的测试框架#xff0c;可让您轻松编写和维护测试。 它专注于为您的应用程序编写… 大约是时候单元测试的开发人员能够使用批注在Parallel中运行测试。 在今天的博客文章中我们将介绍如何使用Easytest提供的注释使传统的Junit测试并行运行。 易测 是在JUnit之上构建的测试框架可让您轻松编写和维护测试。 它专注于为您的应用程序编写数据驱动测试。 让我们首先假设一个类ItemService具有方法2的方法 带有2个参数Double itemId和String itemType的getItems。 API类如下所述。 updateItem接受一个Item实例并在数据库中对其进行更新。 public class ItemService {public List getItems (Double itemId , String itemType);public void updateItem (Item item);} 为了简化示例我们将省略实现。 让我们看一下编写并行单元测试的分步方法。 步骤1 从Maven Central Repository下载EasyTest的最新版本或1.2或更高版本 。 第2步 接下来让我们使用EasyTest批注和数据驱动的测试方法为上述业务方法编写一个简单的单元测试。 RunWith(DataDrivenTestRunner.class)
DataLoader(filePathsgetItemData.csv)
TestConfigProvider(TestConfigProvider.class)
public class ItemServiceTest {Injectprivate ItemService itemService;Beforepublic void before() {System.out.println(BEFORE);}Testpublic List testGetItems(Param(nameitemId)Double itemId , Param(nameitemType)String itemType) {//Actual test conditions hereSystem.out.println(Run testGetItems);}Testpublic void testUpdateItem(Param(nameitem) Item item) {//Actual test conditions hereSystem.out.println(Run testUpdateItem);}Afterpublic void after() {System.out.println(AFTER);}
} 上面的示例使用EasyTest的现有功能例如在测试文件中提供数据并使用CDI批注注入测试bean。 如果您想了解有关EasyTest中的TestConfigProvider和依赖注入的更多信息请参见我以前的博客文章。 如果您想进一步了解如何使用EasyTest编写数据驱动测试则可以访问EasyTest的WIKI Pages 。 现在上面是运行简单的数据驱动单元测试的示例。 以上所有测试将依次进行串行测试。 假设每种测试方法在getItemData.csv文件中指定了两组测试数据则在运行上述测试时我们将在控制台上获得以下内容 BEFORE
Run testGetItems
AFTERBEFORE
Run testGetItems
AFTERBEFORE
Run testUpdateItem
AFTERBEFORE
Run testUpdateItem
AFTER 步骤3 接下来让上述测试并行运行。 只需在类级别包含Parallel批注然后提供要运行的线程数即可。 对于上述测试用例我们将运行两个线程。 RunWith(DataDrivenTestRunner.class)
DataLoader(filePathsgetItemData.csv)
TestConfigProvider(TestConfigProvider.class)
Parallel(threads2)
public class ItemServiceTest {Injectprivate ItemService itemService;Beforepublic void before() {System.out.println(BEFORE);}Testpublic List testGetItems(Param(nameitemId)Double itemId , Param(nameitemType)String itemType) {//Actual test conditions hereSystem.out.println(Run testGetItems);}Testpublic void testUpdateItem(Param(nameitem) Item item) {//Actual test conditions hereSystem.out.println(Run testUpdateItem);}Afterpublic void after() {System.out.println(AFTER);}
} 注意lass级别的Parallel批注。 运行此测试时控制台输出看起来像这样运行相同的测试时可能会有所不同 BEFORE
BEFORE
Run testGetItems
BEFORE
Run testUpdateItem
AFTER
Run testGetItems
BEFORE
AFTER
Run testUpdateItem
AFTER
AFTER 从上面的控制台输出中可以看到测试是同时运行的这就是为什么看到这样的分布式控制台输出的原因。 结论 在上面的示例中我们看到了如何在Parallel中运行测试。 同样当使用Maven等构建工具构建测试时这些测试将并行运行。 这对您具有成千上万的单元测试并且它们需要几分钟的运行时间的场景有很大的帮助。 EasyTest正在使用命令行/系统参数方法来启用/禁用并行功能。 敬请关注。 参考 JavaWorld Blog博客上的JCG合作伙伴 Anuj Kumar 并行运行单元测试 。 翻译自: https://www.javacodegeeks.com/2013/07/run-your-unit-tests-in-parallel.html