Skip to content

Commit

Permalink
Clear ref to current case after test end in reporter
Browse files Browse the repository at this point in the history
  • Loading branch information
schipiga committed Aug 4, 2018
1 parent 587e180 commit 709fe45
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 78 deletions.
21 changes: 3 additions & 18 deletions lib/globals/test.js
@@ -1,30 +1,18 @@
"use strict";

const path = require("path");

const _ = require("lodash");
const U = require("glace-utils");
const LOG = U.logger;

const CONF = require("../config");
const TestCase = require("../testing").TestCase;
const ScopeType = require("../testing").ScopeType;
const setLog = require("../utils").setLog;

const retryTests = require("./session").retryTests;
const _scope = require("./scope");
const scope_ = require("./scope");

let GID = 0;

/**
* Helper to set actual log file.
*
* @function
*/
const setLog = () => {
const testName = CONF.test.curCase ? U.toKebab(CONF.test.curCase.name) : "";
const logFile = path.resolve(CONF.report.logsDir, testName, "logs", "test.log");
LOG.setFile(logFile);
};
setLog(); // Set log immediately.

/**
Expand Down Expand Up @@ -122,7 +110,7 @@ const testFunc = o => {
CONF.counters.testId = o.testCase.id;
CONF.counters.chunkId = 0;

_scope(new ScopeType(o.name).setType("test"), o.testOpts, () => {
scope_(new ScopeType(o.name).setType("test"), o.testOpts, () => {
before(beforeCb(o));
U.wrap(o.fixtures, o.func)();
after(afterCb(o));
Expand Down Expand Up @@ -153,9 +141,6 @@ const afterCb = o => () => {
o.testCase.end(TestCase.PASSED);
}

CONF.test.curCase = null;
setLog(); // Current test case is finished, need to reinit log

if (o.testCase.status !== TestCase.FAILED || o.retries <= 0) {
return;
}
Expand Down
9 changes: 4 additions & 5 deletions lib/reporter/allure.js
Expand Up @@ -50,15 +50,14 @@ module.exports = {
allure.startCase(test.title);
},

testEnd: test => {
const testCase = CONF.test.cases.filter(t => t.name == test.title)[0];
testEnd: () => {

if (testCase.status === TestCase.PASSED) {
if (CONF.test.curCase.status === TestCase.PASSED) {
allure.endCase(allure.PASSED);
}

if (testCase.status === TestCase.FAILED) {
allure.endCase(allure.FAILED, getErrors(testCase));
if (CONF.test.curCase.status === TestCase.FAILED) {
allure.endCase(allure.FAILED, getErrors(CONF.test.curCase));
}
},

Expand Down
7 changes: 7 additions & 0 deletions lib/reporter/base.js
Expand Up @@ -17,6 +17,8 @@ const LOG = U.logger;

const CONF = require("../config");
const TestCase = require("../testing").TestCase;
const setLog = require("../utils").setLog;

/**
* Registered reporters.
*
Expand Down Expand Up @@ -70,6 +72,11 @@ const GlaceReporter = function (runner) {
for (const reporter of reporters) {
if (reporter[methodName]) reporter[methodName](mochaSuite);
}

if (methodName === "testEnd") {
CONF.test.curCase = null;
setLog(); // Current test case is finished, need to reinit log
}
});

runner.on("test", mochaTest => {
Expand Down
31 changes: 16 additions & 15 deletions lib/reporter/testrail.js
Expand Up @@ -90,41 +90,42 @@ module.exports = {
*
* @method
* @instance
* @arg {object} test - `MochaJS` suite.
*/
testEnd: test => {
testEnd: () => {
if (testrail.isFailed) return;
const testCase = CONF.test.cases.filter(t => t.name == test.title)[0];
const testrailCase = cases[test.title];
if (!testrailCase) return;
const testrailCase = cases[CONF.test.curCase.name];
if (!testrailCase) {
LOG.error(`Testrail case '${CONF.test.curCase.name}' is absent`);
return;
}

const testResult = { status_id: Results.PASSED, comment: "" };

if (testCase.screenshots.length) {
if (CONF.test.curCase.screenshots.length) {
testResult.comment += "Screenshots:";
for (const screen of testCase.screenshots) {
for (const screen of CONF.test.curCase.screenshots) {
testResult.comment += "\n" + screen;
}
}
if (testCase.videos.length) {
if (CONF.test.curCase.videos.length) {
testResult.comment += "\n\nVideos:";
for (const video of testCase.videos) {
for (const video of CONF.test.curCase.videos) {
testResult.comment += "\n" + video;
}
}
if (testCase.rawInfo.length) {
if (CONF.test.curCase.rawInfo.length) {
testResult.comment += "\n\nExtra details:";
for (const info of testCase.rawInfo) {
for (const info of CONF.test.curCase.rawInfo) {
testResult.comment += "\n" + info;
}
}
if (testCase.status === TestCase.SKIPPED) {
if (CONF.test.curCase.status === TestCase.SKIPPED) {
testResult.status_id = Results.BLOCKED;
}
if (testCase.status === TestCase.FAILED) {
if (CONF.test.curCase.status === TestCase.FAILED) {
testResult.status_id = Results.FAILED;
testResult.comment += "\n\nErrors:";
for (const error of testCase.errors) {
for (const error of CONF.test.curCase.errors) {
testResult.comment += "\n" + error;
}
}
Expand All @@ -135,7 +136,7 @@ module.exports = {

}).catch(e => {
LOG.error(
`Error to publish test '${test.title}' report to TestRail:`, e);
`Error to publish test '${CONF.test.curCase.name}' report to TestRail:`, e);
});
},
/**
Expand Down
25 changes: 25 additions & 0 deletions lib/utils.js
@@ -0,0 +1,25 @@
"use strict";

/**
* Utils.
*
* @module
*/

const path = require("path");

const U = require("glace-utils");
const LOG = U.logger;

const CONF = require("./config");

/**
* Helper to set actual log file.
*
* @function
*/
module.exports.setLog = () => {
const testName = CONF.test.curCase ? U.toKebab(CONF.test.curCase.name) : "";
const logFile = path.resolve(CONF.report.logsDir, testName, "logs", "test.log");
LOG.setFile(logFile);
};
32 changes: 1 addition & 31 deletions tests/unit/testGlobalsTest.js
Expand Up @@ -23,32 +23,6 @@ suite("globals/test", () => {
test_.__reset__();
});

test("setLog()", () => {
let log,
setLog;

beforeChunk(() => {
log = test_.__get__("LOG");
sandbox.stub(log, "setFile");
setLog = test_.__get__("setLog");
conf.report = { logsDir: "/path/to/report" };
});

chunk("record logs to test dir", () => {
conf.test = { curCase: { name: "my-test" }};
setLog();
expect(log.setFile).to.be.calledOnce;
expect(log.setFile.args[0][0]).to.be.equal("/path/to/report/my-test/logs/test.log");
});

chunk("record logs to common dir", () => {
conf.test = {};
setLog();
expect(log.setFile).to.be.calledOnce;
expect(log.setFile.args[0][0]).to.be.equal("/path/to/report/logs/test.log");
});
});

test("baseTest()", () => {
let baseTest,
testFunc;
Expand Down Expand Up @@ -267,14 +241,12 @@ suite("globals/test", () => {
});

test("afterCb()", () => {
let afterCb, retryTests, setLog, o;
let afterCb, retryTests, o;

beforeChunk(() => {
afterCb = test_.__get__("afterCb");
retryTests = [];
test_.__set__("retryTests", retryTests);
setLog = sinon.spy();
test_.__set__("setLog", setLog);
o = {};
o.testCase = {
errors: ["err"],
Expand All @@ -291,8 +263,6 @@ suite("globals/test", () => {

expect(o.testCase.end).to.be.calledOnce;
expect(o.testCase.end.args[0][0]).to.be.equal("failed");
expect(conf.test.curCase).to.not.exist;
expect(setLog).to.be.calledOnce;
expect(o.retries).to.be.equal(0);
expect(retryTests).to.have.length(1);
});
Expand Down
38 changes: 38 additions & 0 deletions tests/unit/testReporterAllure.js
Expand Up @@ -140,6 +140,44 @@ suite("reporter/allure", () => {
});
});

test("testEnd()", () => {
let conf;

beforeChunk(() => {
allure.endCase = sinon.stub();
allure.PASSED = "passed";
allure.FAILED = "failed";

allureReporter.__set__("getErrors", sinon.stub().returns("errors"));

conf = {
test: {
curCase: {
name: "my test",
status: "passed",
},
},
};
allureReporter.__set__("CONF", conf);
});

chunk("ends passed test", () => {
allureReporter.testEnd();

expect(allure.endCase).to.be.calledOnce;
expect(allure.endCase.args[0][0]).to.be.equal("passed");
});

chunk("ends failed test", () => {
conf.test.curCase.status = "failed";
allureReporter.testEnd();

expect(allure.endCase).to.be.calledOnce;
expect(allure.endCase.args[0][0]).to.be.equal("failed");
expect(allure.endCase.args[0][1]).to.be.equal("errors");
});
});

test("reportSkippedTests()", () => {
let conf, reportSkippedTests;

Expand Down
12 changes: 11 additions & 1 deletion tests/unit/testReporterBase.js
Expand Up @@ -155,12 +155,22 @@ suite("reporter/base", () => {
});

test("on suite end", () => {
let onSuiteEnd, reporters;
let onSuiteEnd, reporters, conf, setLog;

beforeChunk(() => {
onSuiteEnd = methods["suite end"];
reporters = [];
GlaceReporter.__set__("reporters", reporters);

conf = {
test: {
curCase: { name: "my test" },
},
};
GlaceReporter.__set__("CONF", conf);

setLog = sinon.stub();
GlaceReporter.__set__("setLog", setLog);
});

["suite", "scope", "test"].forEach(type => {
Expand Down
16 changes: 8 additions & 8 deletions tests/unit/testReporterTestrail.js
Expand Up @@ -110,13 +110,13 @@ suite("reporter/testrail", () => {
conf = {
testrail: { runId: 123 },
test: {
cases: [{
curCase: {
name: "my test",
screenshots: [],
videos: [],
rawInfo: [],
errors: [],
}],
},
},
};
testrailReporter.__set__("CONF", conf);
Expand Down Expand Up @@ -164,7 +164,7 @@ suite("reporter/testrail", () => {

chunk("sends screenshot paths if there are", async () => {
cases["my test"] = { id: 1 };
conf.test.cases[0].screenshots = ["/path/to/my/screen"];
conf.test.curCase.screenshots = ["/path/to/my/screen"];

testrailReporter.testEnd({ title: "my test" });
await testrailReporter.done();
Expand All @@ -174,7 +174,7 @@ suite("reporter/testrail", () => {

chunk("sends video paths if there are", async () => {
cases["my test"] = { id: 1 };
conf.test.cases[0].videos = ["/path/to/my/video"];
conf.test.curCase.videos = ["/path/to/my/video"];

testrailReporter.testEnd({ title: "my test" });
await testrailReporter.done();
Expand All @@ -184,7 +184,7 @@ suite("reporter/testrail", () => {

chunk("sends raw info if there are", async () => {
cases["my test"] = { id: 1 };
conf.test.cases[0].rawInfo = ["extra data"];
conf.test.curCase.rawInfo = ["extra data"];

testrailReporter.testEnd({ title: "my test" });
await testrailReporter.done();
Expand All @@ -194,7 +194,7 @@ suite("reporter/testrail", () => {

chunk("marks test as blocked if it is skipped", async () => {
cases["my test"] = { id: 1 };
conf.test.cases[0].status = "skipped";
conf.test.curCase.status = "skipped";

testrailReporter.testEnd({ title: "my test" });
await testrailReporter.done();
Expand All @@ -204,8 +204,8 @@ suite("reporter/testrail", () => {

chunk("marks test as failed and attach errors", async () => {
cases["my test"] = { id: 1 };
conf.test.cases[0].status = "failed";
conf.test.cases[0].errors = ["BOOM!"];
conf.test.curCase.status = "failed";
conf.test.curCase.errors = ["BOOM!"];

testrailReporter.testEnd({ title: "my test" });
await testrailReporter.done();
Expand Down

0 comments on commit 709fe45

Please sign in to comment.