diff options
author | Dylan Baker <dylan@pnwbakers.com> | 2016-08-03 16:44:50 -0700 |
---|---|---|
committer | Dylan Baker <dylan@pnwbakers.com> | 2016-08-31 16:04:34 -0700 |
commit | e8d1a39dc050778c6f7da1fe76b5a76b9bd8723c (patch) | |
tree | 658247821915c5dce5aa63793afb089f584cf0ec /unittests | |
parent | d914e30fa26cf945a15f2041a3f20f283b35bc9c (diff) |
framework/backends/junit.py: Add a writer class that handles subtests
This class handles subtests by making a test with subtests into a
testsuite element, and then makes each subtest a testcase element.
Signed-off-by: Dylan Baker <dylanx.c.baker@intel.com>
Acked-by: Jose Fonseca <jfonseca@vmware.com> (v1)
Reviewed-by: Mark Janes <mark.a.janes@intel.com>
Tested-by: Mark Janes <mark.a.janes@intel.com>
Diffstat (limited to 'unittests')
-rw-r--r-- | unittests/framework/backends/test_junit.py | 165 |
1 files changed, 165 insertions, 0 deletions
diff --git a/unittests/framework/backends/test_junit.py b/unittests/framework/backends/test_junit.py index 8f09dac16..6bc9e6aa6 100644 --- a/unittests/framework/backends/test_junit.py +++ b/unittests/framework/backends/test_junit.py @@ -254,3 +254,168 @@ class TestJUnitWriter(object): schema = etree.XMLSchema(file=JUNIT_SCHEMA) # pylint: disable=no-member with open(test_file, 'r') as f: assert schema.validate(etree.parse(f)) + + +class TestJUnitSubtestWriter(object): + """Tests for the JUnitWriter class.""" + + def test_junit_replace(self, tmpdir): + """backends.junit.JUnitBackend.write_test: grouptools.SEPARATOR is + replaced with '.'. + """ + result = results.TestResult() + result.time.end = 1.2345 + result.out = 'this is stdout' + result.err = 'this is stderr' + result.command = 'foo' + result.subtests['foo'] = 'pass' + result.subtests['bar'] = 'fail' + + test = backends.junit.JUnitBackend(six.text_type(tmpdir), + junit_subtests=True) + test.initialize(shared.INITIAL_METADATA) + with test.write_test(grouptools.join('a', 'group', 'test1')) as t: + t(result) + test.finalize() + + test_value = etree.parse(six.text_type(tmpdir.join('results.xml'))) + test_value = test_value.getroot() + + assert test_value.find('.//testsuite/testsuite').attrib['name'] == \ + 'piglit.a.group.test1' + + def test_junit_replace_suffix(self, tmpdir): + """backends.junit.JUnitBackend.write_test: grouptools.SEPARATOR is + replaced with '.'. + """ + result = results.TestResult() + result.time.end = 1.2345 + result.out = 'this is stdout' + result.err = 'this is stderr' + result.command = 'foo' + result.subtests['foo'] = 'pass' + result.subtests['bar'] = 'fail' + + test = backends.junit.JUnitBackend(six.text_type(tmpdir), + junit_subtests=True, + junit_suffix='.foo') + test.initialize(shared.INITIAL_METADATA) + with test.write_test(grouptools.join('a', 'group', 'test1')) as t: + t(result) + test.finalize() + + test_value = etree.parse(six.text_type(tmpdir.join('results.xml'))) + test_value = test_value.getroot() + + suite = test_value.find('.//testsuite/testsuite') + assert suite.attrib['name'] == 'piglit.a.group.test1' + assert suite.find('.//testcase[@name="{}"]'.format('foo.foo')) is not None + + def test_subtest_skip(self, tmpdir): + result = results.TestResult() + result.time.end = 1.2345 + result.out = 'this is stdout' + result.err = 'this is stderr' + result.command = 'foo' + result.subtests['foo'] = 'pass' + result.subtests['bar'] = 'skip' + + test = backends.junit.JUnitBackend(six.text_type(tmpdir), + junit_subtests=True) + test.initialize(shared.INITIAL_METADATA) + with test.write_test(grouptools.join('a', 'group', 'test1')) as t: + t(result) + test.finalize() + + test_value = etree.parse(six.text_type(tmpdir.join('results.xml'))) + test_value = test_value.getroot() + + suite = test_value.find('.//testsuite/testsuite') + assert suite.attrib['name'] == 'piglit.a.group.test1' + assert suite.find('.//testcase[@name="{}"]/skipped'.format('bar')) \ + is not None + + def test_result_skip(self, tmpdir): + result = results.TestResult() + result.time.end = 1.2345 + result.out = 'this is stdout' + result.err = 'this is stderr' + result.command = 'foo' + result.result = 'skip' + + test = backends.junit.JUnitBackend(six.text_type(tmpdir), + junit_subtests=True) + test.initialize(shared.INITIAL_METADATA) + with test.write_test(grouptools.join('a', 'group', 'test1')) as t: + t(result) + test.finalize() + + test_value = etree.parse(six.text_type(tmpdir.join('results.xml'))) + test_value = test_value.getroot() + + elem = test_value.find('.//testsuite/testcase[@name="test1"]/skipped') + assert elem is not None + + def test_classname(self, tmpdir): + result = results.TestResult() + result.time.end = 1.2345 + result.out = 'this is stdout' + result.err = 'this is stderr' + result.command = 'foo' + result.subtests['foo'] = 'pass' + result.subtests['bar'] = 'skip' + + test = backends.junit.JUnitBackend(six.text_type(tmpdir), + junit_subtests=True) + test.initialize(shared.INITIAL_METADATA) + with test.write_test(grouptools.join('a', 'group', 'test1')) as t: + t(result) + test.finalize() + + test_value = etree.parse(six.text_type(tmpdir.join('results.xml'))) + test_value = test_value.getroot() + + suite = test_value.find('.//testsuite/testsuite') + assert suite.find('.//testcase[@classname="piglit.a.group.test1"]') \ + is not None + + class TestValid(object): + @pytest.fixture + def test_file(self, tmpdir): + tmpdir.mkdir('foo') + p = tmpdir.join('foo') + + result = results.TestResult() + result.time.end = 1.2345 + result.out = 'this is stdout' + result.err = 'this is stderr' + result.command = 'foo' + result.pid = 1034 + result.subtests['foo'] = 'pass' + result.subtests['bar'] = 'fail' + + test = backends.junit.JUnitBackend(six.text_type(p), + junit_subtests=True) + test.initialize(shared.INITIAL_METADATA) + with test.write_test(grouptools.join('a', 'group', 'test1')) as t: + t(result) + + result.result = 'fail' + with test.write_test(grouptools.join('a', 'test', 'test1')) as t: + t(result) + test.finalize() + + return six.text_type(p.join('results.xml')) + + def test_xml_well_formed(self, test_file): + """backends.junit.JUnitBackend.write_test: produces well formed xml.""" + etree.parse(test_file) + + @pytest.mark.skipif(etree.__name__ != 'lxml.etree', + reason="This test requires lxml") + def test_xml_valid(self, test_file): + """backends.junit.JUnitBackend.write_test: produces valid JUnit xml.""" + # This XMLSchema class is unique to lxml + schema = etree.XMLSchema(file=JUNIT_SCHEMA) # pylint: disable=no-member + with open(test_file, 'r') as f: + assert schema.validate(etree.parse(f)) |