Skip to content

Instantly share code, notes, and snippets.

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 - - very helpful!
const TestRailAPI = require("testrail-api");
var testRailAPI = new TestRailAPI({
host: "https://<COMPANY>",
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(
{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 => ===;
if (testRailSuite) {
// Grab the test cases for the suite it already exists
testRailCases = (await testRailAPI.getCases(;
} else {
testRailSuite = (await testRailAPI.addSuite({
// 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 => ===;
// If the test case is not found, we create it
if (!testCase) {
testCase = (await testRailAPI.addCase(, {
type: 1,
priority_id: 1
// Add the results for this test case
{ 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.