[英]Run “node test” as part of Visual Studio Team Services build task with results in “tests” tab
I have a project that contains tests that I am running with Mocha from the command line. 我有一个项目,其中包含我从命令行使用Mocha运行的测试。 I have set up a test script in my
packages.json
, which looks as follows: 我在
packages.json
设置了一个测试脚本,如下所示:
"test": "mocha ./**/*.spec.js --reporter dot --require jsdom-global/register"
I have currently got a simple task set up in Visual Studio Team Services, which just runs the npm test
command, this runs Mocha within a console and continues/fails the build depending on whether the tests pass. 我目前在Visual Studio Team Services中设置了一个简单的任务,
npm test
运行npm test
命令,它在控制台中运行Mocha并根据测试是否通过继续/失败构建。
What I'd like to be able to do is have the results of my tests populate the "tests" tab in the build definition after it's run. 我希望能够做的是让我的测试结果在运行后填充构建定义中的“测试”选项卡。 In the same way that I can get this tab populated if I'm running tests on C# code.
就像我在C#代码上运行测试一样,我可以填充此选项卡。
I've tried using Chutzpah for this, but it's overly complicated and seems to require that I jump through all sorts of hoops that mean changing my tests and writing long config files. 我已经尝试过使用Chutzpah,但它过于复杂,似乎要求我跳过各种各样的箍,这意味着改变我的测试并编写长配置文件。 I already have loads of tests written, so really don't want to have to do that.
我已经编写了大量的测试,所以真的不想这样做。 When it did finally discover any of my tests, it complained about
require
and other things related to Node modules. 当它最终发现我的任何测试时,它抱怨了
require
和与Node模块相关的其他事情。
Is what I'm asking for actually possible? 我要求的实际可能吗? Is there a simple way of achieving this that's compatible with running my tests in Node?
有没有一种简单的方法来实现这与在Node中运行我的测试兼容?
I've found a good way of doing it that requires no third-party adapter (eg. Chutzpah). 我发现了一种不需要第三方适配器的好方法(例如Chutzpah)。 It involves getting Mocha to output its report in an XML format, and setting up Visual Studio Team Services to publish the results in an extra step of the build definition.
它涉及让Mocha以XML格式输出报告,并设置Visual Studio Team Services以在构建定义的额外步骤中发布结果。
I installed mocha-junit-reporter
( https://www.npmjs.com/package/mocha-junit-reporter ) and altered my test script to the following: 我安装了
mocha-junit-reporter
( https://www.npmjs.com/package/mocha-junit-reporter )并将我的测试脚本更改为以下内容:
"test": "mocha ./temp/**/*.spec.js --reporter mocha-junit-reporter --require jsdom-global/register"
I then created a new step in my build definition using the "Publish Test Results" task. 然后,我使用“发布测试结果”任务在我的构建定义中创建了一个新步骤。 I set the result format to "JUnit" and added the correct path for the outputted
test-results.xml
file created by the reporter. 我将结果格式设置为“JUnit”,并为报告者创建的输出
test-results.xml
文件添加了正确的路径。
It is worth noting that although Mocha comes with an "XUnit" reporter, this format appears to not work correctly with VSTS even though it's listed as an option. 值得注意的是,虽然Mocha附带了一个“XUnit”记者,但这种格式似乎与VSTS无法正常工作,即使它被列为一个选项。
The results of npm test
now show up in the "tests" tab alongside any other tests from MSTest etc. npm test
的结果现在显示在“测试”选项卡中,与MSTest等的任何其他测试一起显示。
I'm using karma and got this to work in the same way as @dylan-parry suggested. 我正在使用业力并以与@ dylan-parry建议相同的方式工作。 Some excepts below in case it helps others:
下面有一些例外,以防其他人:
package.json 的package.json
"scripts": {
"test": "cross-env NODE_ENV=test karma start"
}
karma.conf.js karma.conf.js
const webpackCfg = require('./webpack.config')('test');
module.exports = function karmaConfig(config) {
config.set({
reporters: ['mocha', 'coverage', 'junit'],
junitReporter: {
outputDir: 'coverage',
outputFile: 'junit-result.xml',
useBrowserName: false
}
})
...
It may also be worth adding I'm using branch policies
on my git branch to prevent PR's being merged if the tests fail, info via this link: 也许值得添加我在我的git分支上使用
branch policies
以防止PR在测试失败时合并,通过以下链接信息:
https://www.visualstudio.com/en-us/docs/git/branch-policies https://www.visualstudio.com/en-us/docs/git/branch-policies
Here's the output in TFS: 这是TFS中的输出:
Next step is to get the coverage working too! 下一步是让覆盖范围也有效!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.