summaryrefslogtreecommitdiff
path: root/unittests
diff options
context:
space:
mode:
authorAndres Gomez <agomez@igalia.com>2020-12-01 10:33:58 +0200
committerAndres Gomez <agomez@igalia.com>2020-12-03 21:46:32 +0200
commit5397bf04f4ccaca8091c358bf17b8df77c0df50b (patch)
treed7242645d32377376c506cf37fd27de3a750c00d /unittests
parente437d5f2b24f92d958bc78ac4c32bcbbecd0721d (diff)
framework: add backends API to write a TestrunResult to a file
Previously, we were just accessing directly some private API. Signed-off-by: Andres Gomez <agomez@igalia.com> Reviewed-by: Dylan Baker <dylan@pnwbakers.com> Part-of: <https://gitlab.freedesktop.org/mesa/piglit/-/merge_requests/424>
Diffstat (limited to 'unittests')
-rw-r--r--unittests/framework/backends/test_json.py63
-rw-r--r--unittests/framework/backends/test_package.py105
2 files changed, 168 insertions, 0 deletions
diff --git a/unittests/framework/backends/test_json.py b/unittests/framework/backends/test_json.py
index 362c85e6f..f91d0fb99 100644
--- a/unittests/framework/backends/test_json.py
+++ b/unittests/framework/backends/test_json.py
@@ -276,6 +276,69 @@ class TestLoadResults(object):
results.TestrunResult)
+class TestWriteResults(object):
+ """Tests for the write_results function."""
+
+ @classmethod
+ def setup_class(cls):
+ """Setup values used by all tests."""
+ test = results.TestrunResult()
+ test.info = {
+ 'system': {
+ 'uname': 'this is uname',
+ 'glxinfo': 'glxinfo',
+ 'clinfo': 'clinfo',
+ 'wglinfo': 'wglinfo',
+ 'lspci': 'this is lspci',
+ }
+ }
+ test.name = 'name'
+ test.options = {'some': 'option'}
+ test.time_elapsed.end = 1.23
+ another_test = results.TestResult()
+ another_test.subtests['foo'] = 'pass'
+ another_test.subtests['bar'] = 'skip'
+ test.tests = {'a test': results.TestResult('pass'),
+ 'another test': another_test}
+
+ cls.test = test
+
+ @pytest.mark.parametrize('filepath', [
+ ('foo.json'),
+ ('bar.json'),
+ ])
+ def test_write(self, filepath, tmpdir):
+ """backends.json.write_results: writes a TestrunResult into a filepath.
+ """
+ p = tmpdir.join(filepath)
+ assert backends.json.write_results(self.test, str(p))
+ assert p.check()
+
+ def test_load(self, tmpdir):
+ """backends.json.write_results: test that the written JSON can be loaded.
+
+ This doesn't attempt to validate the schema of the code. It just
+ attempts a touch test of "can this be read as JSON".
+
+ """
+ p = tmpdir.join('foo.json')
+ assert backends.json.write_results(self.test, str(p))
+
+ with p.open('r') as f:
+ json.load(f)
+
+ @pytest.mark.parametrize('filepath, exception', [
+ ('', FileNotFoundError),
+ (None, TypeError),
+ ])
+ def test_bogus_filepath(self, filepath, exception):
+ """backends.json.write_results: raise with bogus filepaths
+ for the output.
+ """
+ with pytest.raises(exception):
+ assert backends.json.write_results(self.test, filepath)
+
+
class TestLoad(object):
"""Tests for the _load function."""
diff --git a/unittests/framework/backends/test_package.py b/unittests/framework/backends/test_package.py
index 954c036d6..3af360d69 100644
--- a/unittests/framework/backends/test_package.py
+++ b/unittests/framework/backends/test_package.py
@@ -24,6 +24,7 @@
import pytest
from framework import backends
+from framework import results
# pylint: disable=no-self-use
@@ -42,6 +43,7 @@ def mock_backend(mocker):
backend=None,
load=lambda x, _: x,
meta=None,
+ write=None,
)})
yield
@@ -95,6 +97,7 @@ class TestLoad(object):
backend=None,
load=lambda x, _: [x],
meta=None,
+ write=None,
)})
p = tmpdir.join('foo.test_backend')
@@ -128,6 +131,7 @@ class TestLoad(object):
backend=None,
load=None,
meta=None,
+ write=None,
)})
p = tmpdir.join('foo.test_backend')
p.write('foo')
@@ -149,6 +153,7 @@ class TestLoad(object):
backend=None,
load=None,
meta=None,
+ write=None,
)})
with pytest.raises(backends.BackendNotImplementedError):
backends.load('foo.test_backend..gz')
@@ -168,6 +173,105 @@ class TestLoad(object):
backends.load(str(p))
+class TestWrite(object):
+ """Tests for the write function."""
+
+ @classmethod
+ def setup_class(cls):
+ """class fixture."""
+ res = results.TestrunResult()
+ res.tests['foo'] = results.TestResult('pass')
+ res.tests['bar'] = results.TestResult('fail')
+ res.tests['oink'] = results.TestResult('crash')
+ res.tests['bonk'] = results.TestResult('warn')
+ res.tests['bor'] = results.TestResult('crash')
+ res.tests['bor'].subtests['1'] = 'pass'
+ res.tests['bor'].subtests['2'] = 'skip'
+ res.tests['bor'].subtests['3'] = 'fail'
+ res.tests['bor'].subtests['4'] = 'pass'
+
+ cls.results = res
+
+ @pytest.mark.parametrize('expected', [
+ (True),
+ (False),
+ ])
+ def test_basic(self, expected, mocker):
+ """backends.write: works as expected.
+
+ This is an interesting function to test, because it is just a wrapper
+ that returns True or False. So most of the testing should be
+ happening in the tests for each backend.
+
+ However, we can test this by injecting a fake backend, and ensuring
+ that we get back what we expect. What we do is inject list(), which
+ means that we should get back [file_path], instead of just file_path,
+ like the legitimate backends return.
+ """
+ mocker.patch.dict(
+ backends.BACKENDS,
+ {'test_backend': backends.register.Registry(
+ extensions=['.test_backend'],
+ backend=None,
+ load=None,
+ meta=None,
+ write=lambda x, _: expected,
+ )})
+
+ test = backends.write(self.results, 'foo.test_backend')
+ assert expected == test
+
+ @pytest.mark.raises(exception=backends.BackendError)
+ def test_unknown(self, tmpdir): # pylint: disable=unused-argument
+ backends.write(self.results, 'foo.test_extension')
+
+ @pytest.mark.raises(exception=backends.BackendNotImplementedError)
+ def test_notimplemented(self, mocker):
+ """backends.write(): An error is raised if a writer isn't properly
+ implemented.
+ """
+ mocker.patch.dict(
+ backends.BACKENDS,
+ {'test_backend': backends.register.Registry(
+ extensions=['.test_backend'],
+ backend=None,
+ load=None,
+ meta=None,
+ write=None,
+ )})
+
+ backends.write(self.results, 'foo.test_backend')
+
+ @pytest.mark.raises(exception=backends.BackendNotImplementedError)
+ def test_trailing_dot(self, mocker):
+ """framework.backends.write: handles the result name ending in '.'.
+
+ Basically if this reaches a BackendNotImplementedError, then the '.'
+ was handled correctly, otherwise if it's '.' then we should reach the
+ BackendError, which is incorrect.
+ """
+ mocker.patch.dict(
+ backends.BACKENDS,
+ {'test_backend': backends.register.Registry(
+ extensions=['.test_backend'],
+ backend=None,
+ load=None,
+ meta=None,
+ write=None,
+ )})
+
+ backends.write(self.results, 'foo.test_backend..gz')
+
+ @pytest.mark.raises(exception=backends.BackendError)
+ def test_old(self, mock_backend):
+ """backends.write: Ignores files ending in '.old'.
+
+ If this raises a BackendError it means it didn't find a backend to use,
+ thus it skipped the file ending in '.old'.
+ """
+ backends.write(self.results, 'results.test_backend.old')
+
+
class TestSetMeta(object):
"""Tests for the set_meta function."""
@@ -180,6 +284,7 @@ class TestSetMeta(object):
backend=None,
load=None,
meta=lambda x: x.append('bar'),
+ write=None,
)})
test = []