简体   繁体   English

在Visual Studio Team Services Build中启动Mocha测试

[英]Kick off mocha tests in Visual Studio Team Services Build

I can't for the life of me find documentation or a tutorial for kicking off mocha unit tests in Visual Studio Online builds. 我一生都找不到在Visual Studio Online版本中启动Mocha单元测试的文档或教程。

I have node.js app that is building in VSO and being deployed to Azure. 我有在VSO中构建并部署到Azure的node.js应用程序。 That all works wonderfully. 一切都很棒。 I can't seem to figure out how to kick off the spec files through the build process. 我似乎无法弄清楚如何在构建过程中启动规范文件。

How is this done? 怎么做? Is there documentation available somewhere that I'm missing. 我缺少的地方是否有可用的文档。

Assume you have setup Mocha tests with your package.json , ie you run tests with npm test . 假设您使用package.json设置了Mocha测试,即使用npm test For more information, refer to https://docs.npmjs.com/cli/test . 有关更多信息,请参阅https://docs.npmjs.com/cli/test

In your Visual Studio Online build/release: 在您的Visual Studio Online构建/发行版中:

  • Add a "npm" task to install JUnit reporter 添加“ npm”任务以安装JUnit报告器
    • Run custom command install mocha-junit-reporter 运行定制命令install mocha-junit-reporter
  • Add a "npm" task 添加“ npm”任务
    • Run custom command test -- --reporter mocha-junit-reporter 运行自定义命令test -- --reporter mocha-junit-reporter
    • Tips: You may want to increase timeout by adding --timeout 30000 because the build agent maybe running slower than your dev box 提示:您可能要通过添加--timeout 30000来增加超时,因为构建代理的运行速度可能比开发人员盒慢
  • Then, add a "Publish Test Results" task 然后,添加“发布测试结果”任务
    • Set "Test result format" to "JUnit" 将“测试结果格式”设置为“ JUnit”
    • Check the box on "Continue on error" 选中“继续错误”复选框
    • Under "Control Options" > "Run this task", set it to "Even if a previous task has failed, unless the build was canceled" 在“控制选项”>“运行此任务”下,将其设置为“即使先前的任务失败,除非构建被取消”

Queue a build, you should see Mocha test results in your VSO build. 排队构建时,您应该在VSO构建中看到Mocha测试结果。

BONUS! 奖金! You can also add code coverage to your Mocha run with nyc (formerly known as Istanbul ) 您还可以使用nyc (以前称为Istanbul )向Mocha运行中添加代码覆盖率

On top of the steps above: 在上述步骤之上:

  • Install Istanbul locally to your package.json 将Istanbul本地安装到您的package.json
    • Run npm install nyc--save-dev 运行npm install nyc--save-dev
  • Modify your scripts in package.json 修改package.json的脚本
    • Update { "scripts": { "test": "nyc --repoter=cobertura mocha" } } 更新{ "scripts": { "test": "nyc --repoter=cobertura mocha" } }
  • Modify the "npm test" task 修改“ npm测试”任务
    • Run custom command test -- --reporter mocha-junit-reporter 运行自定义命令test -- --reporter mocha-junit-reporter
  • Add a "Publish Code Coverage Results" task 添加“发布代码覆盖率结果”任务
    • Set "Code Coverage Tool" to "Cobertura" 将“代码覆盖率工具”设置为“ Cobertura”
    • Set "Summary File" to $(System.DefaultWorkingDirectory)/coverage/cobertura-coverage.xml 将“摘要文件”设置为$(System.DefaultWorkingDirectory)/coverage/cobertura-coverage.xml
    • Set "Report Directory" to $(System.DefaultWorkingDirectory)/coverage/ 将“报告目录”设置为$(System.DefaultWorkingDirectory)/coverage/
    • Check the box on "Continue on error" 选中“继续错误”复选框
    • Under "Control Options" > "Run this task", set it to "Even if a previous task has failed, unless the build was canceled" 在“控制选项”>“运行此任务”下,将其设置为“即使先前的任务失败,除非构建被取消”
  • Add a new build variable NPM_CONFIG_COVERAGE and set it to true 添加一个新的构建变量NPM_CONFIG_COVERAGE并将其设置为true

Now you got both unit tests and code coverage results in your build report. 现在,您在构建报告中同时获得了单元测试和代码覆盖率结果。

If you've configured you package.json to be able to run tests, adding a npm step that executes npm run test should do it. 如果已将package.json配置为能够运行测试,则添加执行npm run test的npm步骤即可。 If you want to publish the test results you need to make sure that Mocha is writing its results to a format understood by Visual Studio Team Services. 如果要发布测试结果,则需要确保Mocha将其结果写入Visual Studio Team Services可以理解的格式。 JUnit format would be a safe bet. JUnit格式将是一个安全的选择。 Then follow up with a Publish test Results step that uploads the test results. 然后执行“发布测试结果”步骤,以上传测试结果。

You can also use the Visual Studio Test Runner, combined with Chutzpah to run your tests, but I suppose that's going to be a lot of additional work to setup and isn't going to add much. 您还可以结合使用Visual Studio Test Runner和Chutzpah来运行测试,但是我想这将需要大量的设置工作,并且不会增加太多的工作。

After quite a bit of fiddling around i got it to work by adding a "Command line task" to my build definition, i used the following parameters: 经过一番摆弄之后,我通过在构建定义中添加“命令行任务”使其工作,我使用了以下参数:

  • Set Tool to node 将工具设置为node
  • Set Arguments to $(Build.SourcesDirectory)\\node_modules\\jasmine-node\\bin\\jasmine-node --verbose test\\ 将参数设置为$(Build.SourcesDirectory)\\node_modules\\jasmine-node\\bin\\jasmine-node --verbose test\\

My tests are under a "test" folder, also make sure you have jasmine-node as a dev dependency 我的测试位于“测试”文件夹下,还请确保您具有jasmine-node作为开发依赖项 在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运行“节点测试”作为Visual Studio Team Services构建任务的一部分,并在“测试”选项卡中显示结果 - Run “node test” as part of Visual Studio Team Services build task with results in “tests” tab 无法识别Visual Studio Team Services Build yml npm测试 - Visual Studio Team Services Build yml npm test not recognized Visual Studio Team Services上npm的升级节点 - Upgrade node for npm at Visual Studio Team Services npm的Visual Studio Team Services FTP上传输出运行build:prod - Visual Studio Team Services FTP upload output of npm run build:prod 调试mocha测试时,Visual Studio代码中的断点未被命中 - Breakpoints in Visual Studio Code not hit when debugging mocha tests 引用或需要其他文件时,Visual Studio 2019 中的节点 js 应用程序的 Mocha 未在测试资源管理器中显示测试 - Mocha for node js app in Visual Studio 2019 is not showing Tests in Test Explorer when reference or require another file Visual Studio 2017使用Mocha - Visual Studio 2017 Using Mocha Node.js Azure WebJob:如何通过持续交付从Visual Studio Team Services进行部署 - Node.js Azure WebJob: How to deploy from Visual Studio Team Services with Continuous Delivery Visual Studio Team Services Azure部署(无法对指定目录执行操作(“删除文件”)) - Visual Studio Team Services Azure deployment (Unable to perform the operation (“Delete File”) for the specified directory) 使用Mocha或Jasmine对Node.js REST服务进行单元测试 - Unit Tests of Node.js REST services with Mocha or Jasmine
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM