测试报告
<h1>1、内置测试报告</h1>
<p><strong>- spec 格式报告</strong></p>
<pre><code> npx cypress run --reorter=spec</code></pre>
<p><strong>- json 格式报告</strong></p>
<pre><code> npx cypress run --reorter=json</code></pre>
<p><strong>- junit 格式报告</strong></p>
<pre><code> npx cypress run --reorter=junit</code></pre>
<h1>2、mochawesome报告</h1>
<h2>安装</h2>
<pre><code>- npm install --save-dev mocha
- npm install --save-dev mochawesome</code></pre>
<pre><code>yum install --save mocha
yum install --save mochawesome
yum install --save mochawesome-merge
yum install --save mocha-junit-reporter
yum install --save mochawesome-report-generator
yum install --save cypress-multi-reporters</code></pre>
<h2>2.1 配置</h2>
<h3>2.1.1 cypress.json中配置report</h3>
<pre><code class="language-json">{
"reporter":"cypress-multi-reporters",
"reporterOptions":{
"configFile":"reporterOpts.json"
}
}</code></pre>
<pre><code class="language-json">reporterOpts.json
{
"reporterEnabled":"mocha-junit-reporter, mochawesome",
"mochaJunitReporterReporterOptions":{
"mochaFile":"reports/junit/test_results[hash].xml",
"toConsole":true
},
"mochawesomeReporterOptions":{
"reportDir":"reports/mocha",
"quiet":true,
"overwrite":false,
"html":false,
"json":true
}
}</code></pre>
<h3>2.1.2 添加screenshot to report</h3>
<p><a href="https://medium.com/cypress-io-thailand/generate-a-beautiful-test-report-from-running-tests-on-cypress-io-371c00d7865a">https://medium.com/cypress-io-thailand/generate-a-beautiful-test-report-from-running-tests-on-cypress-io-371c00d7865a</a></p>
<pre><code class="language-javascript">const addContext = require('mochawesome/addContext')
Cypress.on('test:after:run', (test, runnable) => {
if (test.state === 'failed') {
const screenshotFileName = `${runnable.parent.title} -- ${test.title} (failed).png`
addContext({ test }, `assets/${Cypress.spec.name}/${screenshotFileName}`)
}
})</code></pre>
<h2>2.2 执行</h2>
<p>npx cypress run --reporter=mochawesome --spec test01.js"</p>
<p>运行后在项目下生成:mochawesome-report文件夹,其中包含json和html格式的报告:</p>
<p><img src="https://www.showdoc.com.cn/server/api/attachment/visitfile/sign/f33ec6322433de3052d1fda5744cc456" alt="" /></p>
<h1>3、生成allure报告</h1>
<h2>3.1 安装</h2>
<p>npm install --save mocha-allure-reporter allure-commandline
<a href="http://allure.qatools.ru/">http://allure.qatools.ru/</a></p>
<h2>3.2 运行</h2>
<p>添加参数:--reporter mocha-allure-reporter
npx cypress run --headless --reporter mocha-allure-reporter --spec cypress/integration/apphub/TC-303-appstore-template-gray-publish.js</p>
<pre><code>allure serve reports</code></pre>
<p>进入测试报告所在文件夹的上一级目录,运行allure命令;其中:serve 后面跟的是报告所在的文件夹名称(junit类型的报告在运行后会在项目下默认生成reports文件夹,内含xml文件,即测试报告);</p>
<h2>3.3 生成报告</h2>
<p>allure generate allure-results --clean -o allure-report && allure open allure-report
(npx allure generate allure-results --clean -o allure-report && npx allure open allure-report)</p>
<h2>3.4 展示</h2>
<p><img src="https://www.showdoc.com.cn/server/api/attachment/visitfile/sign/9f429df8c2c30b83b429923955b6681f" alt="" /></p>
<h2>3.5 错误信息展示</h2>
<p><img src="https://www.showdoc.com.cn/server/api/attachment/visitfile/sign/4b7a594778fca245c942d2cd5f7c91e1" alt="" /></p>
<h1>4、allure+Cypress</h1>
<h1>5. Reference</h1>
<p>[1.] <a href="https://docs.cypress.io/guides/tooling/reporters">https://docs.cypress.io/guides/tooling/reporters</a>
[2.] <a href="https://blog.csdn.net/dawei_yang000000/article/details/111825702">https://blog.csdn.net/dawei_yang000000/article/details/111825702</a>
[3.] <a href="https://blog.csdn.net/u012100968/article/details/107392509">https://blog.csdn.net/u012100968/article/details/107392509</a>
[4.] <a href="http://blog.geveo.com/Allure-Report-Integration-with-Cypress">cypress+allure</a>
[5.] <a href="http://blog.geveo.com/Allure-Report-Integration-with-Cypress">cypress+allure link2</a> method2
[6]. <a href="https://docs.cypress.io/guides/tooling/reporters#Examples">mochawesome cypress.io</a>
[7]. <a href="https://testingadvice.com/how-to-generate-fancy-permanent-cypress-reports/">mochawesome report, link1</a>
[8]. <a href="https://medium.com/cypress-io-thailand/generate-a-beautiful-test-report-from-running-tests-on-cypress-io-371c00d7865a">mochawesome report, link2</a></p>
<h2>Notes</h2>
<p>在生成测试报告的时候要记得修改cypress.json中的文件中:reporter的值;
如:</p>
<ul>
<li>
<p>cypress.json文件中有如下配置</p>
<pre><code>{
"reporter": "junit",
"reporterOptions": {
"mochaFile": "results/test_report_[hash].xml",
"toConsole": true
}
}</code></pre>
</li>
<li>因此如果执行npm run cypress:run --reporter=mochawesome并不会生成mochawesome报告,而是生成junit相关内容,完全根据cypress.json里的配置,使用npm执行测试,修改cypress.json文件里的配置内容,如下所示
<pre><code>{
"reporter": "mochawesome"
}</code></pre></li>
</ul>