我有一组用 mocha / chai-http 编写的API请求,如果出现错误,它不会返回给我有关请求或响应的详细信息。
我的环境是: 摩卡 6.1.4; chai-http 4.3.0
执行命令: mocha --file test.js --reporter mocha-junit-reporter --reporter-options testCaseSwitchClassnameAndName = true,outputs = true,jenkinsMode = true,toConsole = true,includePending = true
如果有问题,我只会收到这样的消息,但是没有请求/响应详细信息,只是断言失败了
<?xml version="1.0" encoding="UTF-8"?>
<testsuites name="Mocha Tests" time="10.007000000000001" tests="3" failures="2" skipped="1">
<testsuite name="Root Suite.API - token && apikey" timestamp="2019-06-12T06:14:07" tests="3" file="/home/test/mocha/test.js" failures="2" time="10.007000000000001" skipped="1">
<testcase name="/rest/login/ should return "token"" time="10.005" classname="API - token && apikey">
<failure message="Timeout of 10000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/home/test/mocha/test.js)" type="Error">
<![CDATA[Error: Timeout of 10000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/home/test/mocha/test.js)]]>
</failure>
</testcase>
</testsuite>
</testsuites>
是否有可能获得包含所有详细信息的类似内容?
<?xml version="1.0" encoding="UTF-8"?>
<testsuite name="examples.ExamplesTest" time="2.175" tests="1" errors="1" skipped="0" failures="0">
<properties>
<property name="java.runtime.name" value="OpenJDK Runtime Environment"/>
</properties>
<testcase name="[1:6] get login token" classname="[demo]" time="2.175">
<error message="demo.feature:10 - javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target" type="Exception">
Exception: demo.feature:10 - javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target at ✽.When method post (demo.feature:10)
</error>
<system-out>
15:17:35.936 [main] DEBUG request: 1
> POST https://10.9.4.13/rest/login/ 1
> Accept-Encoding: gzip,deflate 1
> Connection: Keep-Alive 1
> Content-Length: 42 1
> Content-Type: application/json; charset=UTF-8 1
> Host: 10.9.134.103 1
> User-Agent: Apache-HttpClient/4.5.5 (Java/1.8.0_212) {"username":"admin","password":"admin"}
15:17:37.831 [main] ERROR javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target, http call failed after 1895 milliseconds for URL: https://10.9.4.13/rest/login/
</system-out>
</testcase>
</testsuite>
我的测试如下:
it('/rest/login/ should return "token"', function(done) {
return chai.request(serverurl)
.post('/rest/login/')
.set('Content-Type', 'application/json')
.set('Accept', 'application/json')
.send({username: "admin", password: "admin"})
.then(function(res, err) {
res.should.have.status(200);
res.should.be.json;
res.should.have.cookie('SESSIONID');
res.body.should.be.an('object');
res.body.should.have.property('xsrf');
res.body.xsrf.should.have.property('token');
global.xsrftoken = res.body.xsrf.token;
global.cookie = res.headers['set-cookie'].pop().split(';')[0];
done();
});
});