Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to attach extra info to a test spec?

I have a couple of protractor test scripts. My goal is to generate some reports based on the scripts and the results.

There's have some additional information that I'd like to attach to each test, like an id or reference number. Is there a way to add it to each of the it specs? I don't need jasmine or protractor to do anything with that info, at most just also include it in the test results output files.

I'd like something like this:

describe('Module A Test Suite', function () {
    // note parameter with extra info
    it('This is a test', {testId: 123, release: "v2.0.5"}, function () {

        //note parameter with extra info
        expect({description: "Verify link is shown", priority: 2}, element(by.id('Home')).isPresent()).toBe(true);

        // more tests and expect's here
    }
}

And have some section in the output xml with the extra info.

Maybe result in something like this:

<testsuites>
    <testsuite name="chrome.Module A Test Suite" timestamp="2016-11-22T11:22:45" hostname="localhost" time="77.753" errors="0" tests="8" skipped="0" disabled="0" failures="3">
        <extras testId="123" release="v2.0.5" />
        <testcase classname="chrome.Module A Test Suite" name="This is a test" >
            <extras description="Verify link is shown" priority="2"/>
        </testcase>
    </testsuite>
</testsuites>

If this cannot be added as code itself, is there a way that this can be added as comments or other elements that can be easily parsed? Preferably with an existing tool or jasmine/protractor functionality?

like image 569
frozenkoi Avatar asked Mar 10 '23 07:03

frozenkoi


1 Answers

Regarding the extra information for the it call (the test spec):

Jasmine uses an object result that is part of the test spec and uses it as the result parameter when calling the reporter's specStarted and specDone.

The result object is a property of the object returned from the it function.

Regarding the extra information for the describe call (the test suite):

Jasmine also uses an object result that is part of the test suite, and passes it as the result parameter when calling the reporter's suiteStarted and suiteDone.

The property result for the test suite can be accessed via this inside the function used for describe.

So to assign extra data to it we can do something like

 describe('Module A Test Suite', function () {
    // the following line attaches information to the test suite
    this.result.extra_suite_data = {suiteInfo: "extra info"};

    // note parameter with extra info
    it('This is a test', function () {

        //note parameter with extra info
        expect(element(by.id('Home')).isPresent()).toBe(true);

    })
    .result.extra_spec_data = {testId: 123, release: "v2.0.5"};
    // the line above adds `extra_data` to the `result` property 
    // of the object returned by `it`. Attaching the data to the
    // test spec
});

Adding extra information for the expect statements is a little more complicated, given that the object returned by expect function is not passed to the reporter nor added to the testSpec.result.passedExpectations nor added to the testSpec.result.failedExpectations arrays.

like image 127
frozenkoi Avatar answered Apr 09 '23 16:04

frozenkoi