Syntax
Tip: Click the eyeball icon () in code blocks to toggle the visibility of the hidden lines.
Click here to view Example tests for the following syntax.
If you do not use any Qase syntax, the reporter uses the title from the describe
and it
functions to make the Suite and Test case titles while publishing results.
Import Statement
Add the following statement at the beginning of your spec file, before any tests.
const { qase } = require("mocha-qase-reporter/mocha");
Now, let’s take a closer look at each of the Qase functions.
Qase Id
You can link multiple Qase Ids to a single test. Here’s an example:
it(qase(1, "Test with Qase Id"), function() {
// test logic here
});
Qase Title
The this.title()
method is used to set the title of a test case, both when creating a new test case from the result, and when updating the title of an existing test case - if used with Qase Id.
it("This won't appear in Qase", function() {
this.title("This text will be the title of the test, in Qase");
// test logic here
});
To update a Qase test case’s title from code, you must enable “Update test cases” in your project’s run settings.
If you don’t explicitly set a title using the this.title()
method, the title specified in the it(..)
function will be sent to Qase.
However, if the this.title
method is defined, it always takes precedence and overrides the title from the test(..)
function.
Steps
The reporter uses the title from the this.step
function as the step title. By providing clear and descriptive step names, you make it easier to understand the test’s flow when reviewing the test case.
Additionally, these steps get their own result in the Qase Test run, offering a well-organized summary of the test flow. This helps quickly identify the cause of any failures.
it("A Test case with steps, updated from code", function() {
this.step("Initialize the environment", function() {
// Set up test environment
});
this.step("Test Core Functionality of the app", function () {
// Exercise core functionality
});
this.step("Verify Expected Behavior of the app", function () {
// Assert expected behavior
});
this.step("Verify if user is able to log out successfully", function () {
// Expected user to be logged out (but, ran into a problem!).
});
assert.strictEqual(1, 1);
});
Fields
Currently, you can define description
, pre & post conditions
, and fields like severity
, priority
, layer
with this method, allowing you to specify and maintain the context of the case, all directly from within your code.
it("Test case with fields", function() {
this.fields({
priority: "high",
severity: "critical",
layer: "api",
description: "Write your description here..",
preconditions: "Write your preconditions here..",
postconditions: "Write your postconditions here..",
});
// test logic here
});
Suite
You can use this method to nest the resulting test cases in a particular suite. There’s something to note here – suites, unlike test cases, are not identified uniquely by the Reporter.
Therefore, when defining an existing suite - the title of the suite is used for matching.
it("Test with a defined suite", function() {
this.suite("Example suite");
// test logic here
});
it("Test within multiple levels of suite", function() {
this.suite(
"Example suite\tChild Suite",
);
// test logic here
});
Parameters
Parameters are a great way to make your tests more dynamic, reusable, and data-driven. By defining parameters in this method, you can ensure only one test case with all the parameters is created in your Qase project, avoiding duplication.
const testCases = [
{ browser: "Chromium", username: "@alice", password: "123" },
{ browser: "Firefox", username: "@bob", password: "456" },
{ browser: "Webkit", username: "@charlie", password: "789" },
];
describe("Example param.spec.js Single Parameter", function() {
testCases.forEach(({ browser }) => {
it(`Test login with ${browser}`, function() {
this.title("Verify if login page loads successfully");
//Instead of creating three separate test cases in Qase, this method will add a 'browser' parameter, with three values.
this.parameters({ Browser: browser });
// test logic here
});
});
});
describe("Example param.spec.js Group Parameter", function() {
testCases.forEach(({ username, password }) => {
it(`Test login with ${username} using qase.groupParameters`, function() {
this.title("Verify if user is able to login with their username.");
//Here, we're grouping the username and password parameters to track them together, as a set of parameters for the test.
//This will show the username and password combinations for the test.
this.groupParameters({
Username: username,
Password: password,
});
// test logic here
});
});
});
Comment
In addition to this.step()
, this method can be used to provide any additional context to your test, it helps maintiain the code by clarifying the expected result of the test.
it("Test with Qase comment", function() {
/*
* Please note, this comment is added to a Result, not to the Test case.
*/
this.comment("This comment will be added to the test result");
// test logic here
});
Attach
This method can help attach one, or more files to the test’s result. You can also add the file’s contents, directly from code. For example:
it("Test result with attachment", function() {
To attach a single file
this.attach({ paths: "./test/examples/attachments/test-file.txt" });
// Add multiple attachments.
this.attach({
paths: ["/path/to/file", "/path/to/another/file"],
});
// Upload file's contents directly from code.
this.attach({
name: "attachment.txt",
content: "Example text",
contentType: "text/plain",
});
// test logic here
});
Ignore
If this method is added, the reporter will exclude the test’s result from the report sent to Qase. While the test will still executed by mocha, its result will not be considered by the reporter.
it("This test is executed using Mocha; however, it is NOT reported to Qase", function() {
this.ignore();
// test logic here
});