Debian Codegraph @

README.md

raw
1
2 subunit: A streaming protocol for test results
3 Copyright (C) 2005-2013 Robert Collins <[email protected]>
4
5 Licensed under either the Apache License, Version 2.0 or the BSD 3-clause
6 license at the users choice. A copy of both licenses are available in the
7 project source as Apache-2.0 and BSD. You may not use this file except in
8 compliance with one of these two licences.
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under these licenses is distributed on an "AS IS" BASIS, WITHOUT
12 WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 license you chose for the specific language governing permissions and
14 limitations under that license.
15
16 See the COPYING file for full details on the licensing of Subunit.
17
18 # Subunit
19
20 Subunit is a streaming protocol for test results.
21
22 There are two major revisions of the protocol. Version 1 was trivially human
23 readable but had significant defects as far as highly parallel testing was
24 concerned - it had no room for doing discovery and execution in parallel,
25 required substantial buffering when multiplexing and was fragile - a corrupt
26 byte could cause an entire stream to be misparsed. Version 1.1 added
27 encapsulation of binary streams which mitigated some of the issues but the
28 core remained.
29
30 Version 2 shares many of the good characteristics of Version 1 - it can be
31 embedded into a regular text stream (e.g. from a build system) and it still
32 models xUnit style test execution. It also fixes many of the issues with
33 Version 1 - Version 2 can be multiplexed without excessive buffering (in
34 time or space), it has a well defined recovery mechanism for dealing with
35 corrupted streams (e.g. where two processes write to the same stream
36 concurrently, or where the stream generator suffers a bug).
37
38 More details on both protocol version s can be found in the 'Protocol' section
39 of this document.
40
41 Subunit comes with command line filters to process a subunit stream and
42 language bindings for python, C, C++ and shell. Bindings are easy to write
43 for other languages.
44
45 A number of useful things can be done easily with subunit:
46 * Test aggregation: Tests run separately can be combined and then
47 reported/displayed together. For instance, tests from different languages
48 can be shown as a seamless whole, and tests running on multiple machines
49 can be aggregated into a single stream through a multiplexer.
50 * Test archiving: A test run may be recorded and replayed later.
51 * Test isolation: Tests that may crash or otherwise interact badly with each
52 other can be run separately and then aggregated, rather than interfering
53 with each other or requiring an adhoc test->runner reporting protocol.
54 * Grid testing: subunit can act as the necessary serialisation and
55 deserialiation to get test runs on distributed machines to be reported in
56 real time.
57
58 Subunit supplies the following filters:
59 * tap2subunit - convert perl's TestAnythingProtocol to subunit.
60 * subunit2csv - convert a subunit stream to csv.
61 * subunit2disk - export a subunit stream to files on disk.
62 * subunit2pyunit - convert a subunit stream to pyunit test results.
63 * subunit2gtk - show a subunit stream in GTK.
64 * subunit2junitxml - convert a subunit stream to JUnit's XML format.
65 * subunit-diff - compare two subunit streams.
66 * subunit-filter - filter out tests from a subunit stream.
67 * subunit-ls - list info about tests present in a subunit stream.
68 * subunit-stats - generate a summary of a subunit stream.
69 * subunit-tags - add or remove tags from a stream.
70
71 ## Integration with other tools
72
73
74 Subunit's language bindings act as integration with various test runners like
75 'check', 'cppunit', Python's 'unittest'. Beyond that a small amount of glue
76 (typically a few lines) will allow Subunit to be used in more sophisticated
77 ways.
78
79 ### Python
80
81 Subunit has excellent Python support: most of the filters and tools are written
82 in python and there are facilities for using Subunit to increase test isolation
83 seamlessly within a test suite.
84
85 The most common way is to run an existing python test suite and have it output
86 subunit via the ``subunit.run`` module::
87
88 $ python -m subunit.run mypackage.tests.test_suite
89
90 For more information on the Python support Subunit offers , please see
91 ``pydoc subunit``, or the source in ``python/subunit/``
92
93 ### Rust
94
95 There is a rust crate [subunit](https://crates.io/crates/subunit) that can be used to parse v1 and v1, and emit v2 subunit streams.
96
97
98 ### C
99
100 Subunit has C bindings to emit the protocol. The 'check' C unit testing project
101 has included subunit support in their project for some years now. See
102 'c/README' for more details.
103
104 ### C++
105
106
107 The C library is includable and usable directly from C++. A TestListener for
108 CPPUnit is included in the Subunit distribution. See 'c++/README' for details.
109
110 ### shell
111
112 There are two sets of shell tools. There are filters, which accept a subunit
113 stream on stdin and output processed data (or a transformed stream) on stdout.
114
115 Then there are unittest facilities similar to those for C : shell bindings
116 consisting of simple functions to output protocol elements, and a patch for
117 adding subunit output to the 'ShUnit' shell test runner. See 'shell/README' for
118 details.
119
120 ### perl
121
122 There is a perl module [Test::Subunit](https://metacpan.org/pod/Test::Subunit)
123 that can be used to emit and consume subunit v1 streams.
124
125 #### Filter recipes
126
127 To ignore some failing tests whose root cause is already known::
128
129 subunit-filter --without 'AttributeError.*flavor'
130
131 ## The xUnit test model
132
133
134 Subunit implements a slightly modified xUnit test model. The stock standard
135 model is that there are tests, which have an id(), can be run, and when run
136 start, emit an outcome (like success or failure) and then finish.
137
138 Subunit extends this with the idea of test enumeration (find out about tests
139 a runner has without running them), tags (allow users to describe tests in
140 ways the test framework doesn't apply any semantic value to), file attachments
141 (allow arbitrary data to make analysing a failure easy) and timestamps.
142
143 ## The protocol
144
145
146 Version 2, or v2 is new and still under development, but is intended to
147 supersede version 1 in the very near future. Subunit's bundled tools accept
148 only version 2 and only emit version 2, but the new filters subunit-1to2 and
149 subunit-2to1 can be used to interoperate with older third party libraries.
150
151 ### Version 2
152
153 Version 2 is a binary protocol consisting of independent packets that can be
154 embedded in the output from tools like make - as long as each packet has no
155 other bytes mixed in with it (which 'make -j N>1' has a tendency of doing).
156 Version 2 is currently in draft form, and early adopters should be willing
157 to either discard stored results (if protocol changes are made), or bulk
158 convert them back to v1 and then to a newer edition of v2.
159
160 The protocol synchronises at the start of the stream, after a packet, or
161 after any 0x0A byte. That is, a subunit v2 packet starts after a newline or
162 directly after the end of the prior packet.
163
164 Subunit is intended to be transported over a reliable streaming protocol such
165 as TCP. As such it does not concern itself with out of order delivery of
166 packets. However, because of the possibility of corruption due to either
167 bugs in the sender, or due to mixed up data from concurrent writes to the same
168 fd when being embedded, subunit strives to recover reasonably gracefully from
169 damaged data.
170
171 A key design goal for Subunit version 2 is to allow processing and multiplexing
172 without forcing buffering for semantic correctness, as buffering tends to hide
173 hung or otherwise misbehaving tests. That said, limited time based buffering
174 for network efficiency is a good idea - this is ultimately implementer
175 choice. Line buffering is also discouraged for subunit streams, as dropping
176 into a debugger or other tool may require interactive traffic even if line
177 buffering would not otherwise be a problem.
178
179 In version two there are two conceptual events - a test status event and a file
180 attachment event. Events may have timestamps, and the path of multiplexers that
181 an event is routed through is recorded to permit sending actions back to the
182 source (such as new tests to run or stdin for driving debuggers and other
183 interactive input). Test status events are used to enumerate tests, to report
184 tests and test helpers as they run. Tests may have tags, used to allow
185 tunnelling extra meanings through subunit without requiring parsing of
186 arbitrary file attachments. Things that are not standalone tests get marked
187 as such by setting the 'Runnable' flag to false. (For instance, individual
188 assertions in TAP are not runnable tests, only the top level TAP test script
189 is runnable).
190
191 File attachments are used to provide rich detail about the nature of a failure.
192 File attachments can also be used to encapsulate stdout and stderr both during
193 and outside tests.
194
195 Most numbers are stored in network byte order - Most Significant Byte first
196 encoded using a variation of http://www.dlugosz.com/ZIP2/VLI.html. The first
197 byte's top 2 high order bits encode the total number of octets in the number.
198 This encoding can encode values from 0 to 2**30-1, enough to encode a
199 nanosecond. Numbers that are not variable length encoded are still stored in
200 MSB order.
201
202 +--------+--------+---------+------------+
203 | prefix | octets | max | max |
204 +========+========+=========+============+
205 | 00 | 1 | 2**6-1 | 63 |
206 +--------+--------+---------+------------+
207 | 01 | 2 | 2**14-1 | 16383 |
208 +--------+--------+---------+------------+
209 | 10 | 3 | 2**22-1 | 4194303 |
210 +--------+--------+---------+------------+
211 | 11 | 4 | 2**30-1 | 1073741823 |
212 +--------+--------+---------+------------+
213
214 All variable length elements of the packet are stored with a length prefix
215 number allowing them to be skipped over for consumers that don't need to
216 interpret them.
217
218 UTF-8 strings are with no terminating NUL and should not have any embedded NULs
219 (implementations SHOULD validate any such strings that they process and take
220 some remedial action (such as discarding the packet as corrupt).
221
222 In short the structure of a packet is:
223
224 PACKET := SIGNATURE FLAGS PACKET_LENGTH TIMESTAMP? TESTID? TAGS? MIME?
225 FILECONTENT? ROUTING_CODE? CRC32
226
227 In more detail...
228
229 Packets are identified by a single byte signature - 0xB3, which is never legal
230 in a UTF-8 stream as the first byte of a character. 0xB3 starts with the first
231 bit set and the second not, which is the UTF-8 signature for a continuation
232 byte. 0xB3 was chosen as 0x73 ('s' in ASCII') with the top two bits replaced by
233 the 1 and 0 for a continuation byte.
234
235 If subunit packets are being embedded in a non-UTF-8 text stream, where 0x73 is
236 a legal character, consider either recoding the text to UTF-8, or using
237 subunit's 'file' packets to embed the text stream in subunit, rather than the
238 other way around.
239
240 Following the signature byte comes a 16-bit flags field, which includes a
241 4-bit version field - if the version is not 0x2 then the packet cannot be
242 read. It is recommended to signal an error at this point (e.g. by emitting
243 a synthetic error packet and returning to the top level loop to look for
244 new packets, or exiting with an error). If recovery is desired, treat the
245 packet signature as an opaque byte and scan for a new synchronisation point.
246 NB: Subunit V1 and V2 packets may legitimately included 0xB3 internally,
247 as they are an 8-bit safe container format, so recovery from this situation
248 may involve an arbitrary number of false positives until an actual packet
249 is encountered : and even then it may still be false, failing after passing
250 the version check due to coincidence.
251
252 Flags are stored in network byte order too.
253
254 ```asciiart
255 +------------+------------+------------------------+
256 | High byte | Low byte |
257 +------------+------------+------------------------+
258 | 15 14 13 12 11 10 9 8 | 7 6 5 4 3 2 1 0 |
259 +------------+------------+------------------------+
260 | VERSION | feature bits |
261 +------------+-------------------------------------+
262 ```
263
264 Valid version values are:
265 `0x2` - version 2
266
267 Feature bits:
268
269 | Bit position | Mask | Description|
270 |---------|-------------|---------------------------|
271 | Bit 11 | `0x0800` | Test id present. |
272 | Bit 10 | `0x0400` | Routing code present. |
273 | Bit 9 | `0x0200` | Timestamp present. |
274 | Bit 8 | `0x0100` | Test is 'runnable'. |
275 | Bit 7 | `0x0080` | Tags are present. |
276 | Bit 6 | `0x0040` | File content is present. |
277 | Bit 5 | `0x0020` | File MIME type is present.|
278 | Bit 4 | `0x0010` | EOF marker. |
279 | Bit 3 | `0x0008` | Must be zero in version 2.|
280
281 Test status gets three bits:
282 Bit 2 | Bit 1 | Bit 0 - mask 0x0007 - A test status enum lookup:
283
284 * 000 - undefined / no test
285 * 001 - Enumeration / existence
286 * 002 - In progress
287 * 003 - Success
288 * 004 - Unexpected Success
289 * 005 - Skipped
290 * 006 - Failed
291 * 007 - Expected failure
292
293 After the flags field is a number field giving the length in bytes for the
294 entire packet including the signature and the checksum. This length must
295 be less than 4MiB - 4194303 bytes. The encoding can obviously record a larger
296 number but one of the goals is to avoid requiring large buffers, or causing
297 large latency in the packet forward/processing pipeline. Larger file
298 attachments can be communicated in multiple packets, and the overhead in such a
299 4MiB packet is approximately 0.2%.
300
301 The rest of the packet is a series of optional features as specified by the set
302 feature bits in the flags field. When absent they are entirely absent.
303
304 Forwarding and multiplexing of packets can be done without interpreting the
305 remainder of the packet until the routing code and checksum (which are both at
306 the end of the packet). Additionally, routers can often avoid copying or moving
307 the bulk of the packet, as long as the routing code size increase doesn't force
308 the length encoding to take up a new byte (which will only happen to packets
309 less than or equal to 16KiB in length) - large packets are very efficient to
310 route.
311
312 Timestamp when present is a 32 bit unsigned integer for seconds, and a variable
313 length number for nanoseconds, representing UTC time since Unix Epoch in
314 seconds and nanoseconds.
315
316 Test id when present is a UTF-8 string. The test id should uniquely identify
317 runnable tests such that they can be selected individually. For tests and other
318 actions which cannot be individually run (such as test
319 fixtures/layers/subtests) uniqueness is not required (though being human
320 meaningful is highly recommended).
321
322 Tags when present is a length prefixed vector of UTF-8 strings, one per tag.
323 There are no restrictions on tag content (other than the restrictions on UTF-8
324 strings in subunit in general). Tags have no ordering.
325
326 When a MIME type is present, it defines the MIME type for the file across all
327 packets same file (routing code + testid + name uniquely identifies a file,
328 reset when EOF is flagged). If a file never has a MIME type set, it should be
329 treated as application/octet-stream.
330
331 File content when present is a UTF-8 string for the name followed by the length
332 in bytes of the content, and then the content octets.
333
334 If present routing code is a UTF-8 string. The routing code is used to
335 determine which test backend a test was running on when doing data analysis,
336 and to route stdin to the test process if interaction is required.
337
338 Multiplexers SHOULD add a routing code if none is present, and prefix any
339 existing routing code with a routing code ('/' separated) if one is already
340 present. For example, a multiplexer might label each stream it is multiplexing
341 with a simple ordinal ('0', '1' etc), and given an incoming packet with route
342 code '3' from stream '0' would adjust the route code when forwarding the packet
343 to be '0/3'.
344
345 Following the end of the packet is a CRC-32 checksum of the contents of the
346 packet including the signature.
347
348 #### Example packets
349
350 Trivial test "foo" enumeration packet, with test id, runnable set,
351 status=enumeration. Spaces below are to visually break up signature / flags /
352 length / testid / crc32
353
354 b3 2901 0c 03666f6f 08555f1b
355
356
357 ### Version 1 (and 1.1)
358
359 Version 1 (and 1.1) are mostly human readable protocols.
360
361 #### Sample subunit wire contents
362
363 The following:
364
365 ```subunit
366
367 test: test foo works
368 success: test foo works
369 test: tar a file.
370 failure: tar a file. [
371 ..
372 ].. space is eaten.
373 foo.c:34 WARNING foo is not defined.
374 ]
375 a writeln to stdout
376
377 When run through subunit2pyunit::
378
379 .F
380 a writeln to stdout
381
382 ========================
383 FAILURE: tar a file.
384 -------------------
385 ..
386 ].. space is eaten.
387 foo.c:34 WARNING foo is not defined.
388 ```
389
390 #### Subunit v1 protocol description
391
392 This description is being ported to an EBNF style. Currently its only partly in
393 that style, but should be fairly clear all the same. When in doubt, refer the
394 source (and ideally help fix up the description!). Generally the protocol is
395 line orientated and consists of either directives and their parameters, or
396 when outside a DETAILS region unexpected lines which are not interpreted by
397 the parser - they should be forwarded unaltered:
398
399 ```ebnf
400 test|testing|test:|testing: test LABEL
401 success|success:|successful|successful: test LABEL
402 success|success:|successful|successful: test LABEL DETAILS
403 failure: test LABEL
404 failure: test LABEL DETAILS
405 error: test LABEL
406 error: test LABEL DETAILS
407 skip[:] test LABEL
408 skip[:] test LABEL DETAILS
409 xfail[:] test LABEL
410 xfail[:] test LABEL DETAILS
411 uxsuccess[:] test LABEL
412 uxsuccess[:] test LABEL DETAILS
413 progress: [+|-]X
414 progress: push
415 progress: pop
416 tags: [-]TAG ...
417 time: YYYY-MM-DD HH:MM:SSZ
418
419 LABEL: UTF8*
420 NAME: UTF8*
421 DETAILS ::= BRACKETED | MULTIPART
422 BRACKETED ::= '[' LF UTF8-lines ']' LF
423 MULTIPART ::= '[ multipart' LF PART* ']' LF
424 PART ::= PART_TYPE LF NAME LF PART_BYTES LF
425 PART_TYPE ::= Content-Type: type/sub-type(;parameter=value,parameter=value)
426 PART_BYTES ::= (DIGITS CR LF BYTE{DIGITS})* '0' CR LF
427 LF ::= '\n'
428 CR ::= '\r'
429 ```
430
431 unexpected output on stdout -> stdout.
432 exit w/0 or last test completing -> error
433
434 Tags given outside a test are applied to all following tests
435 Tags given after a test: line and before the result line for the same test
436 apply only to that test, and inherit the current global tags.
437 A '-' before a tag is used to remove tags - e.g. to prevent a global tag
438 applying to a single test, or to cancel a global tag.
439
440 The progress directive is used to provide progress information about a stream
441 so that stream consumer can provide completion estimates, progress bars and so
442 on. Stream generators that know how many tests will be present in the stream
443 should output "progress: COUNT". Stream filters that add tests should output
444 "progress: +COUNT", and those that remove tests should output
445 "progress: -COUNT". An absolute count should reset the progress indicators in
446 use - it indicates that two separate streams from different generators have
447 been trivially concatenated together, and there is no knowledge of how many
448 more complete streams are incoming. Smart concatenation could scan each stream
449 for their count and sum them, or alternatively translate absolute counts into
450 relative counts inline. It is recommended that outputters avoid absolute counts
451 unless necessary. The push and pop directives are used to provide local regions
452 for progress reporting. This fits with hierarchically operating test
453 environments - such as those that organise tests into suites - the top-most
454 runner can report on the number of suites, and each suite surround its output
455 with a (push, pop) pair. Interpreters should interpret a pop as also advancing
456 the progress of the restored level by one step. Encountering progress
457 directives between the start and end of a test pair indicates that a previous
458 test was interrupted and did not cleanly terminate: it should be implicitly
459 closed with an error (the same as when a stream ends with no closing test
460 directive for the most recently started test).
461
462 The time directive acts as a clock event - it sets the time for all future
463 events. The value should be a valid ISO8601 time.
464
465 The skip, xfail and uxsuccess outcomes are not supported by all testing
466 environments. In Python the testttools (https://launchpad.net/testtools)
467 library is used to translate these automatically if an older Python version
468 that does not support them is in use. See the testtools documentation for the
469 translation policy.
470
471 skip is used to indicate a test was discovered but not executed. xfail is used
472 to indicate a test that errored in some expected fashion (also know as "TODO"
473 tests in some frameworks). uxsuccess is used to indicate and unexpected success
474 where a test though to be failing actually passes. It is complementary to
475 xfail.
476
477 ## Hacking on subunit
478
479 ### Releases
480
481 * Update versions in `configure.ac` and `python/subunit/__init__.py`.
482 * Update `NEWS`.
483 * Do a ``make distcheck``, which will update `Makefile` etc.
484 * Push a tagged commit — this triggers the `publish.yml` GitHub Actions workflow,
485 which publishes to PyPI automatically via trusted publishing (OIDC), no API token needed:
486 ``git push --tags origin master:master``