Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Adding test runs and test results from Bespoken test execution to TestRail
// Get the test rail project ID from an environment variable
const PROJECT_ID = process.env.PROJECT_ID;
// We use the node-testrail-api project - https://github.com/rundef/node-testrail-api - very helpful!
const TestRailAPI = require("testrail-api");
var testRailAPI = new TestRailAPI({
host: "https://<COMPANY>.testrail.com",
user: "<USERNAME>",
password: "<PASSWORD_OR_API_KEY>"
});
let runID;
let testRailSuite; // The current test suite from TestRail being processed - global variable for caching purposes
let testRailCases; // The current test cases from TestRail being processed - global variable for caching purposes
module.exports = {
// This is called once when we first start the whole suite of tests
initialize: () => {
// Create a new run
const result = await testRail.addRun(
PROJECT_ID,
{name: "Bespoken Sample Test Run"}
);
// Save off the run ID created by this first step
runID = result.body.runID;
},
// Before the test suite is executed, a test run is created on testRail
onTestSuiteStart: async (testSuite) => {
// When we run a suite, we check to see if it has already been setup in TestRail
// If not, then we create it
const testRailSuites = (await testRailAPI.getSuites(PROJECT_ID)).body;
testRailSuite = testRailSuites.find(o => o.name === testSuite.name);
if (testRailSuite) {
// Grab the test cases for the suite it already exists
testRailCases = (await testRailAPI.getCases(testRailSuite.id)).body;
} else {
testRailSuite = (await testRailAPI.addSuite({
name: testSuite.name,
description: testSuite.name
})).body;
}
},
// After the test suite is executed, the results are added on testRail
onTestSuiteEnd: (testResults) => {
// We loop through each result, and record it in TestRail
testResults.forEach((result) => {
bespokenTestResult = result.passed ? 1 : 5; // 1 equals to Passed and 5 equals to Failed on TestRail
// Look up the case associated with this test
let testCase = testRailCases.find(case => case.name === result.test.name);
// If the test case is not found, we create it
if (!testCase) {
testCase = (await testRailAPI.addCase(testRailSuite.id, {
title: result.test.name,
type: 1,
priority_id: 1
}).body;
}
// Add the results for this test case
testRailAPI.addResultForCase(
runID,
testCase.id,
{ status_id: bespokenTestResult }
);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.