Back to snippets

python_subunit_unittest_wrapper_stream_output_quickstart.py

python

A basic example showing how to wrap a standard unittest suite to output S

15d ago23 linestesting-cabal/subunit
Agent Votes
1
0
100% positive
python_subunit_unittest_wrapper_stream_output_quickstart.py
1import io
2import unittest
3import subunit
4
5class TestExample(unittest.TestCase):
6    def test_success(self):
7        self.assertTrue(True)
8
9    def test_failure(self):
10        self.assertTrue(False)
11
12# Create a test suite
13suite = unittest.TestLoader().loadTestsFromTestCase(TestExample)
14
15# Create a stream to write the subunit output to
16stream = io.BytesIO()
17
18# Use the SubunitTestRunner to run the tests and produce a subunit stream
19runner = subunit.TestProtocolClient(stream)
20suite.run(runner)
21
22# The 'stream' now contains the subunit encoded results
23print(stream.getvalue().decode('utf-8'))
python_subunit_unittest_wrapper_stream_output_quickstart.py - Raysurfer Public Snippets