ResultFormats.rst 6.8 KB
Newer Older
1 2
.. _output-plugins:

3
Result Formats
4 5 6
==============

A test runner must provide an assortment of ways to clearly communicate results
7 8 9 10 11 12
to interested parties, be them humans or machines.

Results for human beings
------------------------

Avocado has two different result formats that are intended for human beings:
13

14 15 16
* Its default UI, which shows the live test execution results on a command
  line, text based, UI.
* The HTML report, which is generated after the test job finishes running.
17

18 19
Avocado command line UI
~~~~~~~~~~~~~~~~~~~~~~~
20

21 22
A regular run of Avocado will present the test results in a live fashion,
that is, the job and its test(s) results are constantly updated::
23

24
    $ avocado run sleeptest failtest synctest
25 26 27 28
    JOB ID    : 5ffe479262ea9025f2e4e84c4e92055b5c79bdc9
    JOB LOG   : $HOME/avocado/job-results/job-2014-08-12T15.57-5ffe4792/job.log
    JOB HTML  : $HOME/avocado/job-results/job-2014-08-12T15.57-5ffe4792/html/results.html
    TESTS     : 3
29 30 31
    (1/3) sleeptest.1: PASS (1.01 s)
    (2/3) failtest.1: FAIL (0.00 s)
    (3/3) synctest.1: PASS (1.98 s)
32 33 34 35 36 37 38
    PASS      : 1
    ERROR     : 1
    FAIL      : 1
    SKIP      : 0
    WARN      : 0
    INTERRUPT : 0
    TIME      : 3.17 s
39 40

The most important thing is to remember that programs should never need to parse
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
human output to figure out what happened to a test job run.

HTML report
~~~~~~~~~~~

As can be seen in the previous example, Avocado shows the path to an HTML
report that will be generated as soon as the job finishes running::

    $ avocado run sleeptest failtest synctest
    ...
    JOB HTML  : $HOME/avocado/job-results/job-2014-08-12T15.57-5ffe4792/html/results.html
    ...

You can also request that the report be opened automatically by using the
``--open-browser`` option. For example::

    $ avocado run sleeptest --open-browser
58

59 60 61 62 63 64 65 66 67 68 69 70 71 72
Will show you the nice looking HTML results report right after ``sleeptest``
finishes running.

Machine readable results
------------------------

Another type of results are those intended to be parsed by other
applications. Several standards exist in the test community, and Avocado can
in theory support pretty much every result standard out there.

Out of the box, Avocado supports a couple of machine readable results.

xunit
~~~~~
73

74
The default machine readable output in Avocado is
75 76 77 78 79 80
`xunit <http://help.catchsoftware.com/display/ET/JUnit+Format>`__.

xunit is an XML format that contains test results in a structured form, and
are used by other test automation projects, such as `jenkins
<http://jenkins-ci.org/>`__. If you want to make Avocado to generate xunit
output in the standard output of the runner, simply use::
81

82
    $ scripts/avocado --xunit - run "sleeptest failtest synctest"
83 84 85 86 87 88 89 90
    <?xml version="1.0" encoding="UTF-8"?>
    <testsuite name="avocado" tests="3" errors="0" failures="1" skip="0" time="2.88632893562" timestamp="2014-04-24 18:25:39.545588">
        <testcase classname="sleeptest" name="sleeptest.1" time="1.10091400146"/>
        <testcase classname="failtest" name="failtest.1" time="0.0921177864075">
            <failure><![CDATA[This test is supposed to fail]]></failure>
        </testcase>
        <testcase classname="synctest" name="synctest.1" time="1.69329714775"/>

91 92
Note the dash `-` in the option `--xunit`, it means that the xunit result
should go to the standard output.
93

94 95
json
~~~~
96 97

`JSON <http://www.json.org/>`__ is a widely used data exchange format. The
98
json Avocado plugin outputs job information, similarly to the xunit output
99 100
plugin::

101
    $ scripts/avocado --json - run "sleeptest failtest synctest"
102
    {"tests": [{"test": "sleeptest.1", "url": "sleeptest", "status": "PASS", "time": 1.4282619953155518}, {"test": "failtest.1", "url": "failtest", "status": "FAIL", "time": 0.34017300605773926}, {"test": "synctest.1", "url": "synctest", "status": "PASS", "time": 2.109131097793579}], "errors": 0, "skip": 0, "time": 3.87756609916687, "debuglog": "$HOME/avocado/logs/run-2014-06-11-01.35.15/debug.log", "pass": 2, "failures": 1, "total": 3}
103

104 105
Note the dash `-` in the option `--json`, it means that the xunit result
should go to the standard output.
106

107 108 109 110 111
Bear in mind that there's no documented standard for the Avocado JSON result
format. This means that it will probably grow organically to acommodate
newer Avocado features. A reasonable effort will be made to not break
backwards compatibility with applications that parse the current form of its
JSON result.
112

113 114
Silent result
~~~~~~~~~~~~~
115

116 117
While not a very fancy result format, an application may want nothing but
the exit status code from an Avocado test job run. Example::
118

119
    $ avocado --silent run failtest
120 121 122
    $ echo $?
    1

123 124 125 126 127 128 129 130 131 132
In practice, this would usually be used by scripts that will in turn run
Avocado and check its results::

    #!/bin/bash
    ...
    avocado run /path/to/my/test.py --silent
    if [ $? == 0 ]; then
       echo "great success!"
    elif
       ...
133

134 135 136 137 138 139
Multiple results at once
------------------------

You can have multiple results formats at once, as long as only one of them
uses the standard output. For example, it is fine to use the xunit result on
stdout and the JSON result to output to a file::
140

141
    $ scripts/avocado --xunit - --json /tmp/result.json run "sleeptest synctest"
142 143 144 145 146 147 148
    <?xml version="1.0" encoding="UTF-8"?>
    <testsuite name="avocado" tests="2" errors="0" failures="0" skip="0" time="3.21392536163" timestamp="2014-06-11 01:49:35.858187">
        <testcase classname="sleeptest" name="sleeptest.1" time="1.34533214569"/>
        <testcase classname="synctest" name="synctest.1" time="1.86859321594"/>
    </testsuite>

    $ cat /tmp/result.json
149
    {"tests": [{"test": "sleeptest.1", "url": "sleeptest", "status": "PASS", "time": 1.345332145690918}, {"test": "synctest.1", "url": "synctest", "status": "PASS", "time": 1.8685932159423828}], "errors": 0, "skip": 0, "time": 3.213925361633301, "debuglog": "$HOME/avocado/logs/run-2014-06-11-01.49.35/debug.log", "pass": 2, "failures": 0, "total": 2}
150

151
But you won't be able to do the same without the --json flag passed to
152 153
the program::

154
    $ scripts/avocado --xunit - --json - run "sleeptest synctest"
155 156 157
    Avocado could not set --json and --xunit both to output to stdout.
    Please set the output flag of one of them to a file to avoid conflicts.

158
That's basically the only rule, and a sane one, that you need to follow.
159

160
Implementing other result formats
161 162
---------------------------------

163 164 165 166 167 168 169 170 171
If you are looking to implement a new machine or human readable output
format, you can refer to :mod:`avocado.core.plugins.xunit` and use it as a
starting point.

In a nutshell, you have to implement a class that inherits from
:class:`avocado.core.result.TestResult` and implements all public methods,
that perform actions (write to a file/stream) for each test states. You can
take a look at :doc:`Plugins` for more information on how to write a plugin
that will activate and execute the new result format.