做我男朋友的网站,淘宝网免费素材图库,传媒公司网站php源码,wordpress4.9.6中文我最近在一个宠物项目中着手构建自动化的UI#xff08;集成#xff09;测试以及普通的单元测试。 我想将所有这些集成到我的Maven构建中#xff0c;并提供代码覆盖率报告#xff0c;以便我可以了解测试覆盖率不足的区域。 我不仅发布了项目的源代码#xff0c;还整理了一个… 我最近在一个宠物项目中着手构建自动化的UI集成测试以及普通的单元测试。 我想将所有这些集成到我的Maven构建中并提供代码覆盖率报告以便我可以了解测试覆盖率不足的区域。 我不仅发布了项目的源代码还整理了一个简单的示例来演示如何获得所有这些设置。 因此如果您希望集成maven junit webdriver 现在为selenium和emma 请继续阅读以了解我的工作方式。 首先所有的源代码都可以在github上找到 https : //github.com/activelylazy/coverage-example 。 我将显示关键片段但显然有很多细节被忽略了希望如此不相关。 示例应用 该示例应用程序不是打破传统而是一个简单的即使有点人为的问候世界 怎么运行的 起始页面是指向hello world页面的简单链接 h1Example app/h1
pSee the a idmessageLink hrefhelloWorld.htmlmessage/a/p Hello World页面仅显示以下消息 h1Example app/h1
p idmessagec:out value${message}//p hello world控制器渲染视图并传递消息 public class HelloWorldController extends ParameterizableViewController {// Our message factoryprivate MessageFactory messageFactory;Overrideprotected ModelAndView handleRequestInternal(HttpServletRequest request,HttpServletResponse response) throws Exception {// Get the success viewModelAndView mav super.handleRequestInternal(request, response);// Add our messagemav.addObject(message,messageFactory.createMessage());return mav;}Autowiredpublic void setMessageFactory(MessageFactory messageFactory) {this.messageFactory messageFactory;}
} 最后MessageFactory仅返回硬编码的消息 public String createMessage() {return Hello world;
}单元测试 我们定义了一个简单的单元测试以验证MessageFactory的行为是否符合预期 public class MessageFactoryTest {// The message factoryprivate MessageFactory messageFactory;Testpublic void testCreateMessage() {assertEquals(Hello world,messageFactory.createMessage());}Autowiredpublic void setMessageFactory(MessageFactory messageFactory) {this.messageFactory messageFactory;}
}建立 一个基本的maven pom文件足以构建此文件并运行单元测试。 至此我们有了一个正在运行的应用程序并对我们可以构建和运行的核心功能例如它进行了单元测试。 projectmodelVersion4.0.0/modelVersiongroupIdcom.example/groupIdartifactIdhelloworld/artifactIdpackagingwar/packagingversion1.0-SNAPSHOT/versionnamehelloworld Maven Webapp/namebuildfinalNamehelloworld/finalName/builddependencies...omitted.../dependencies
/project代码覆盖率 现在我们集成Emma以便获得一些代码覆盖率报告。 首先我们定义一个新的Maven配置文件这使我们可以控制是否在任何给定的版本上使用emma。 profileidwith-emma/idbuildpluginsplugingroupIdorg.codehaus.mojo/groupIdartifactIdemma-maven-plugin/artifactIdinheritedtrue/inheritedexecutionsexecutionidinstrument/idphaseprocess-test-classes/phasegoalsgoalinstrument/goal/goals/execution/executions/plugin/plugins/build
/profile 这只是在Maven“过程测试类”阶段调用“仪器”目标。 即一旦我们编译了类文件请使用emma对其进行检测。 我们可以通过使用新的配置文件调用Maven来运行它 mvn clean install -Pwith-emma 构建完成后我们可以运行Emma生成代码覆盖率报告 在Windows上 java -cp %USERPROFILE%/.m2/repository/emma/emma/2.0.5312/emma-2.0.5312.jar emma report -r xml,html -in coverage.ec -in target/coverage.em 在Linux上 java -cp ~/.m2/repository/emma/emma/2.0.5312/emma-2.0.5312.jar emma report -r xml,html -in coverage.ec -in target/coverage.em 现在我们可以在coverage / index.html中查看HTML覆盖率报告。 在这一点上它表明我们有50的测试覆盖率按类。 MessageFactory已完全覆盖但是HelloWorldController根本没有任何测试。 整合测试 为了测试我们的控制器和JSP我们将使用WebDriver创建一个简单的集成测试。 这是一个恰巧启动浏览器的JUnit测试。 public class HelloWorldIntegrationTest {// The webdriverprivate static WebDriver driver;BeforeClasspublic static void initWebDriver() {driver new FirefoxDriver();}AfterClasspublic static void stopSeleniumClent() {try {driver.close();driver.quit();} catch( Throwable t ) {// Catch error log, not critical for testsSystem.err.println(Error stopping driver: t.getMessage());t.printStackTrace(System.err);}}Testpublic void testHelloWorld() {// Start from the homepagedriver.get(http://localhost:9080/helloworld/);HomePage homePage new HomePage(driver);HelloWorldPage helloWorldPage homePage.clickMessageLink();assertEquals(Hello world,helloWorldPage.getMessage());}
} 第4-18行只是在测试之前启动Web驱动程序并在测试完成后将其关闭关闭浏览器窗口。 在第22行我们使用硬编码的URL导航到主页。 在第23行我们初始化主页的Web Driver 页面对象 。 这封装了页面工作方式的所有细节从而使测试可以与页面进行功能上的交互而无需担心机制使用哪些元素等。 在第24行我们使用主页对象单击“消息”链接 这将导航到hello world页面。 在第25行我们确认Hello World页面上显示的消息是我们期望的。 注意我正在使用页面对象将测试规范 做什么与测试实现 如何做分开。 有关为什么这很重要的更多信息请参见防止测试变脆 。 主页 主页对象非常简单 public HelloWorldPage clickMessageLink() {driver.findElement(By.id(messageLink)).click();return new HelloWorldPage(driver);
}HelloWorldPage hello world页面同样简单 public String getMessage() {return driver.findElement(By.id(message)).getText();
}运行集成测试 要在我们的Maven构建过程中运行集成测试我们需要进行一些更改。 首先我们需要从单元测试阶段中排除集成测试 plugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-surefire-plugin/artifactId...configuration...excludesexclude**/*IntegrationTest.java/excludeexclude**/common/*/exclude/excludes/configuration
/plugin 然后我们定义一个新的配置文件因此我们可以选择运行集成测试 profileidwith-integration-tests/idbuildpluginsplugingroupIdorg.mortbay.jetty/groupIdartifactIdmaven-jetty-plugin/artifactIdversion6.1.22/versionconfigurationscanIntervalSeconds5/scanIntervalSecondsstopPort9966/stopPortstopKeyfoo/stopKeyconnectorsconnector implementationorg.mortbay.jetty.nio.SelectChannelConnectorport9080/portmaxIdleTime60000/maxIdleTime/connector/connectors/configurationexecutionsexecutionidstart-jetty/idphasepre-integration-test/phasegoalsgoalrun/goal/goalsconfigurationdaemontrue/daemon/configuration/executionexecutionidstop-jetty/idphasepost-integration-test/phasegoalsgoalstop/goal/goals/execution/executions/pluginplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-surefire-plugin/artifactIdversion2.5/versioninheritedtrue/inheritedexecutionsexecutionidintegration-tests/idphaseintegration-test/phasegoalsgoaltest/goal/goalsconfigurationexcludesexclude**/common/*/exclude/excludesincludesinclude**/*IntegrationTest.java/include/includes/configuration/execution/executions/plugin/plugins/build
/profile 个人资料 id with-integration-tests / id 内部版本 插件 插件 groupId org.mortbay.jetty / groupId artifactId maven-jetty-plugin / artifactId version 6.1.22 / version 配置 scanIntervalSeconds 5 / scanIntervalSeconds stopPort 9966 / stopPort stopKey foo / stopKey 连接器 连接器实现” org.mortbay.jetty.nio.SelectChannelConnector” port $ {test.server.port} / port maxIdleTime 60000 / maxIdleTime / connector / connectors / configuration 执行 执行 id开始码头/ id phase集成前测试/ phase 目标 goal运行/ goal / goals 配置 daemon true / daemon / configuration / execution 执行 id停止码头/ id phase集成后测试/ phase 目标 goal停止/ goal / goals / execution / executions / plugin 插件 groupId org.apache.maven.plugins / groupId artifactId maven-surefire-plugin / artifactId version 2.5 / version inherited true / inherited 执行 执行 id集成测试/ id phase集成测试/ phase 目标 goal测试/ goal / goals 配置 排除 exclude ** / common / * / exclude / excludes 包括 include ** / * IntegrationTest.java / include / includes / configuration / execution / executions / plugin / plugins / build / profile 这可能看起来很复杂但实际上我们只是在配置码头来运行我们的集成测试。 然后配置如何自行运行集成测试。 在9-19行中配置码头-要继续运行的港口以及如何停止码头。 21-30行配置了码头以在Maven构建的“集成前测试”阶段运行。 31-37行配置了要在Maven构建的“集成后测试”阶段停止的码头。 在第40-62行中我们再次使用maven-surefire-plugin这次是在构建的“集成测试”阶段运行仅运行我们的集成测试类。 我们可以使用以下命令运行此构建 mvn clean install -Pwith-emma -Pwith-integration-tests 这将构建所有内容运行单元测试构建战争启动码头以主持战争运行我们的集成测试在其余运行时您将看到一个Firefox窗口弹出然后关闭码头。 因为战争是通过检测类构建的所以在我们运行集成测试时Emma还会跟踪代码覆盖率。 现在我们可以构建应用程序运行单元测试和集成测试收集组合的代码覆盖率报告。 如果我们重新运行emma报告并检查代码覆盖率我们现在将看到我们具有100的测试覆盖率-因为控制器也已通过测试覆盖。 问题 有哪些未解决的问题可以做哪些进一步的扩展 该构建会生成一个已检测到的WAR –这意味着您需要运行第二个构建没有emma才能获得可用于生产的构建。 集成测试对Jetty配置为启动的端口进行硬编码。 意味着测试不能直接在Eclipse中运行。 可以传入此端口默认为8080这样集成测试就可以通过maven构建在Eclipse中运行。 在构建服务器上运行时您可能不希望Firefox随机弹出如果甚至安装了X 因此运行xvfb是一个好主意。 可以将maven设置为在集成测试之前和之后启动和停止xvfb。 参考 单元和集成测试的代码覆盖范围以及Actively Lazy博客的JCG合作伙伴 Dave提供的信息 相关文章 任何软件开发公司应存在的服务实践和工具第1部分 任何软件开发公司应存在的服务实践和工具第2部分 这是在您的业务逻辑之前 您的代码中有几个错误 使用FindBugs产生更少的错误代码 Java工具源代码优化和分析 每个程序员都应该知道的事情 为什么自动化测试可以提高您的开发速度 翻译自: https://www.javacodegeeks.com/2011/10/code-coverage-with-unit-integration.html