我正在尝试在mocha-allure-reporter中使用Cypress。我将mocha
和mocha-allure-reporter
安装为开发依赖项,并在mocha-allure-reporter
中将cypress.json
称为报告者。
我尝试了example section of mocha allure page中引用的以下代码:
require('mocha-allure-reporter');
describe("simple test demo", () => {
const testStep = allure.createStep("initial", () => {
console.log("First Test")
});
it("simple passed test", () => {
testStep();
});
}
但是,出现以下错误:
未捕获的TypeError:无法读取未定义的属性“ Base”
...在第一行本身:
require('mocha-allure-reporter')
在控制台上查看时,我发现该错误源于Allure记者的第var Base = require("mocha").reporters.Base
行:
var Base = require("mocha").reporters.Base;
var Allure = require("allure-js-commons");
...
...
global.allure = new Runtime(allureReporter);
/**
* Initialize a new `Allure` test reporter.
*
* @param {Runner} runner
* @param {Object} opts mocha options
* @api public
*/
function AllureReporter(runner, opts) {
...
...
请注意,执行完成后,将在allure-results
目录中创建以下输出xml文件。
<?xml version='1.0'?>
<ns2:test-suite xmlns:ns2='urn:model.allure.qatools.yandex.ru' start='1547481439243' stop='1547481439477'>
<name></name>
<title></title>
<test-cases>
<test-case start='1547481439282' status='broken' stop='1547481439460'>
<name>An uncaught error was detected outside of a test</name>
<title>An uncaught error was detected outside of a test</title>
<labels/>
<parameters/>
<steps/>
<attachments/>
<failure>
<message>Cannot read property 'Base' of undefined
This error originated from your test code, not from Cypress.
When Cypress detects uncaught errors originating from your test code it will automatically fail the current test.
Cypress could not associate this error to any specific test.
We dynamically generated a new test to display this failure.</message>
<stack-trace>Uncaught TypeError: Cannot read property 'Base' of undefined
This error originated from your test code, not from Cypress.
When Cypress detects uncaught errors originating from your test code it will automatically fail the current test.
Cypress could not associate this error to any specific test.
We dynamically generated a new test to display this failure.
at Object.<anonymous> (http://localhost:61925/__cypress/tests?p=cypress\integration\Tests\Test.spec.js-289:15125:38)
at Object.98.allure-js-commons (http://localhost:61925/__cypress/tests?p=cypress\integration\Tests\Test.spec.js-289:15201:4)
at o (http://localhost:61925/__cypress/tests?p=cypress\integration\Tests\Test.spec.js-289:1:265)
at http://localhost:61925/__cypress/tests?p=cypress\integration\Tests\Test.spec.js-289:1:316
at Object.40.mocha-allure-reporter (http://localhost:61925/__cypress/tests?p=cypress\integration\Tests\Test.spec.js-289:7566:1)
at o (http://localhost:61925/__cypress/tests?p=cypress\integration\Tests\Test.spec.js-289:1:265)
at r (http://localhost:61925/__cypress/tests?p=cypress\integration\Tests\Test.spec.js-289:1:431)
at http://localhost:61925/__cypress/tests?p=cypress\integration\Tests\Test.spec.js-289:1:460</stack-trace>
</failure>
</test-case>
</test-cases>
</ns2:test-suite>
请指导我。谢谢!
答案 0 :(得分:1)
我能够通过简单地安装 mocha-allure-reporter (连同mocha)来使用
npm install mocha mocha-allure-reporter
并遵照赛普拉斯npm记者here的赛普拉斯指南在package.json
中设置脚本
"scripts": {
...
"cypress:run": "cypress run --reporter mocha-allure-reporter"
请注意,我认为这些报告器仅使用Cypress的“运行”命令,而不使用Cypress的“打开”命令。
输出是一个名为“ allure-results”的文件夹,其中包含一堆xml文件。我认为这些都可以使用Allure框架工具进行显示。
示例输出文件:
<?xml version='1.0'?>
<ns2:test-suite xmlns:ns2='urn:model.allure.qatools.yandex.ru' start='1547254197911' stop='1547254201289'>
<name>Tasks Page</name>
<title>Tasks Page</title>
<test-cases>
<test-case start='1547254199721' status='passed' stop='1547254199815'>
<name>should have a title</name>
<title>should have a title</title>
<labels/>
<parameters/>
<steps/>
<attachments/>
</test-case>
</test-cases>
</ns2:test-suite>
要运行魅力代码,您需要通过cy.task
访问nodejs上下文。
例如
/cypress/plugins/index.js
require('mocha-allure-reporter');
module.exports = (on) => {
on('task', {
allureTestStep () {
const testStep = allure.createStep("initial", () => {
console.log("First Test")
});
testStep()
return null
}
})
}
规格
describe("simple test demo", () => {
it("simple passed test", () => {
cy.task('allureTestStep')
});
})
注意,这会在您启动Cypress的命令窗口中生成控制台日志,而不是浏览器控制台。
但是,您可以将值从任务传递回测试中,具体取决于您实际要执行的操作(有关详细信息,请参阅文档)。