[Piglit] [PATCH 39/49] unittests: port backend_tests to pytest

Dylan Baker dylan at pnwbakers.com
Fri Jul 29 18:39:25 UTC 2016


Signed-off-by: Dylan Baker <dylanx.c.baker at intel.com>
---
 unittests/backends_tests.py                  | 234 ---------------------------
 unittests/framework/backends/test_package.py | 203 +++++++++++++++++++++++
 unittests/junit_backends_tests.py            |   6 +-
 3 files changed, 208 insertions(+), 235 deletions(-)
 delete mode 100644 unittests/backends_tests.py
 create mode 100644 unittests/framework/backends/test_package.py

diff --git a/unittests/backends_tests.py b/unittests/backends_tests.py
deleted file mode 100644
index 41eb5b6..0000000
--- a/unittests/backends_tests.py
+++ /dev/null
@@ -1,234 +0,0 @@
-# Copyright (c) 2014 Intel Corporation
-
-# Permission is hereby granted, free of charge, to any person obtaining a copy
-# of this software and associated documentation files (the "Software"), to deal
-# in the Software without restriction, including without limitation the rights
-# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-# copies of the Software, and to permit persons to whom the Software is
-# furnished to do so, subject to the following conditions:
-
-# The above copyright notice and this permission notice shall be included in
-# all copies or substantial portions of the Software.
-
-# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-# SOFTWARE.
-
-# pylint: disable=missing-docstring
-
-""" Tests for the backend package """
-
-from __future__ import (
-    absolute_import, division, print_function, unicode_literals
-)
-import os
-
-import nose.tools as nt
-import six
-
-from framework import backends, options
-from . import utils
-
-
-BACKEND_INITIAL_META = {
-    'name': 'name',
-    'test_count': 0,
-    'env': {},
-    'options': {k: v for k, v in options.OPTIONS},
-}
-
-
-# Helpers
-
-def _notimplemented_setup():
-    """Setup function that injects a new test Registry into the BACKENDS
-    variable.
-
-    should be used in conjunction with the _registry_teardown method.
-
-    """
-    backends.BACKENDS['test_backend'] = backends.register.Registry(
-        extensions=['.test_backend'],
-        backend=None,
-        load=None,
-        meta=None,
-    )
-
-
-def _registry_teardown():
-    """Remove the test_backend Register from backends.BACKENDS."""
-    del backends.BACKENDS['test_backend']
-
-
-# Tests
-
-
- at utils.nose.generator
-def test_get_backend():
-    """ Generate tests to get various backends """
-    # We use a hand generated list here to ensure that we are getting what we
-    # expect
-    backends_ = {
-        'json': backends.json.JSONBackend,
-        'junit': backends.junit.JUnitBackend,
-    }
-
-    def check(n, i):
-        return nt.assert_is(backends.get_backend(n), i)
-
-    for name, inst in six.iteritems(backends_):
-        check.description = \
-            'backends.get_backend({0}): returns {0} backend'.format(name)
-        yield check, name, inst
-
-
- at nt.raises(backends.BackendError)
-def test_get_backend_unknown():
-    """backends.get_backend: An error is raised with an unknown backend."""
-    backends.get_backend('obviously fake backend')
-
-
- at nt.raises(backends.BackendNotImplementedError)
- at nt.with_setup(_notimplemented_setup, _registry_teardown)
-def test_get_backend_notimplemented():
-    """backends.get_backend: An error is raised if a backend isn't implemented.
-    """
-    backends.get_backend('test_backend')
-
-
- at nt.with_setup(teardown=_registry_teardown)
- at utils.nose.test_in_tempdir
-def test_load():
-    """backends.load(): works as expected.
-
-    This is an interesting function to test, because it is just a wrapper that
-    returns a TestrunResult object. 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 menas that we
-    should get back [file_path].
-
-    """
-    backends.BACKENDS['test_backend'] = backends.register.Registry(
-        extensions=['.test_extension'],
-        backend=None,
-        load=lambda x, y: [x],  # y is for a compression value
-        meta=None,
-    )
-
-    file_path = 'foo.test_extension'
-    with open(file_path, 'w') as f:
-        f.write('foo')
-
-    test = backends.load(file_path)
-    nt.assert_list_equal([file_path], test)
-
-
- at nt.raises(backends.BackendError)
- at utils.nose.test_in_tempdir
-def test_load_unknown():
-    """backends.load(): An error is raised if no modules supportes `extension`
-    """
-    file_path = 'foo.test_extension'
-
-    with open(file_path, 'w') as f:
-        f.write('foo')
-    backends.load(file_path)
-
-
- at utils.nose.no_error
- at nt.with_setup(_notimplemented_setup, _registry_teardown)
- at utils.nose.test_in_tempdir
-def test_load_resume():
-    """backends.load: works for resuming (no extension known)."""
-    backends.BACKENDS['test_backend'] = backends.register.Registry(
-        extensions=['.test_backend'],
-        backend=None,
-        load=lambda x, y: x,
-        meta=None,
-    )
-    os.mkdir('tests')
-    name = os.path.join('tests', '0.test_backend')
-    with open(name, 'w') as f:
-        f.write('foo')
-
-    backends.load('.')
-
-
- at nt.raises(backends.BackendNotImplementedError)
- at nt.with_setup(_notimplemented_setup, _registry_teardown)
- at utils.nose.test_in_tempdir
-def test_load_notimplemented():
-    """backends.load(): An error is raised if a loader isn't properly implmented.
-    """
-    file_path = 'foo.test_backend'
-    with open(file_path, 'w') as f:
-        f.write('foo')
-
-    backends.load(file_path)
-
-
-def test_set_meta():
-    """backends.set_meta(): Works as expected."""
-    backends.BACKENDS['test_backend'] = backends.register.Registry(
-        extensions=None,
-        backend=None,
-        load=None,
-        meta=lambda x: x.append('bar'),
-    )
-
-    test = ['foo']
-
-    backends.set_meta('test_backend', test)
-    nt.assert_list_equal(test, ['foo', 'bar'])
-
-
- at nt.raises(backends.BackendError)
-def test_set_meta_no_backened():
-    """backends.set_meta: raises an error if there is no meta function."""
-    backends.set_meta('foo', {})
-
-
- at nt.raises(backends.BackendNotImplementedError)
- at nt.with_setup(_notimplemented_setup, _registry_teardown)
-def test_set_meta_notimplemented():
-    """backends.load(): An error is raised if a set_meta isn't properly implmented.
-    """
-    backends.set_meta('test_backend', {})
-
-
- at nt.with_setup(_notimplemented_setup, _registry_teardown)
- at nt.raises(backends.BackendNotImplementedError)
- at utils.nose.not_raises(backends.BackendError)
-def test_load_trailing_dot():
-    """framework.backends.load: 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.
-
-    """
-    backends.load('foo.test_backend..gz')
-
-
- at nt.with_setup(_notimplemented_setup, _registry_teardown)
- at utils.nose.test_in_tempdir
- at nt.raises(backends.BackendError)
-def test_load_old():
-    """backends.load(): 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'.
-
-    """
-    os.mkdir('test')
-    file_path = os.path.join('test', 'results.test_backend.old')
-    with open(file_path, 'w') as f:
-        f.write('foo')
-
-    backends.load('test')
diff --git a/unittests/framework/backends/test_package.py b/unittests/framework/backends/test_package.py
new file mode 100644
index 0000000..d668899
--- /dev/null
+++ b/unittests/framework/backends/test_package.py
@@ -0,0 +1,203 @@
+# Copyright (c) 2014, 2016 Intel Corporation
+
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+# SOFTWARE.
+
+""" Tests for the backend package """
+
+from __future__ import (
+    absolute_import, division, print_function, unicode_literals
+)
+
+import pytest
+import six
+
+from framework import backends
+
+# pylint: disable=no-self-use
+
+
+
+# Helpers
+
+
+ at pytest.yield_fixture
+def mock_backend(mocker):
+    """Add an extra backend for testing."""
+    mocker.patch.dict(
+        backends.BACKENDS,
+        {'test_backend': backends.register.Registry(
+            extensions=['.test_backend'],
+            backend=None,
+            load=lambda x, _: x,
+            meta=None,
+        )})
+    yield
+
+
+# Tests
+
+
+class TestGetBackend(object):
+    """Tests for the get_backend function."""
+
+    @pytest.mark.parametrize("name,expected", [
+        ('json', backends.json.JSONBackend),
+        ('junit', backends.junit.JUnitBackend),
+    ])
+    def test_basic(self, name, expected):
+        """Test that ensures the expected input and output."""
+        assert backends.get_backend(name) is expected
+
+    def test_unknown_backend(self):
+        """backends.get_backend: An error is raised with an unknown backend."""
+        with pytest.raises(backends.BackendError):
+            backends.get_backend('obviously fake backend')
+
+    def test_notimplemented(self, mock_backend):  # pylint: disable=redefined-outer-name,unused-argument
+        """backends.get_backend: An error is raised if a backend isn't
+        implemented.
+        """
+        with pytest.raises(backends.BackendNotImplementedError):
+            backends.get_backend('test_backend')
+
+
+class TestLoad(object):
+    """Tests for the load function."""
+
+    def test_basic(self, mocker, tmpdir):  # pylint: disable=unused-argument
+        """backends.load: works as expected.
+
+        This is an interesting function to test, because it is just a wrapper
+        that returns a TestrunResult object. 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=lambda x, _: [x],
+                meta=None,
+            )})
+
+        p = tmpdir.join('foo.test_backend')
+        p.write('foo')
+        test = backends.load(six.text_type(p))
+        assert [six.text_type(p)] == test
+
+    def test_unknown(self, tmpdir):
+        p = tmpdir.join('foo.test_extension')
+        p.write('foo')
+
+        with pytest.raises(backends.BackendError):
+            backends.load(six.text_type(p))
+
+    def test_interupted(self, tmpdir, mock_backend):  # pylint: disable=unused-argument,redefined-outer-name
+        """backends.load: works for resuming (no extension known)."""
+        tmpdir.mkdir('tests')
+        with tmpdir.join('tests', '0.test_backend').open('w') as f:
+            f.write('foo')
+
+        backends.load(six.text_type(tmpdir))
+
+    def test_notimplemented(self, tmpdir, mocker):
+        """backends.load(): An error is raised if a loader isn't properly
+        implmented.
+        """
+        mocker.patch.dict(
+            backends.BACKENDS,
+            {'test_backend': backends.register.Registry(
+                extensions=['.test_backend'],
+                backend=None,
+                load=None,
+                meta=None,
+            )})
+        p = tmpdir.join('foo.test_backend')
+        p.write('foo')
+
+        with pytest.raises(backends.BackendNotImplementedError):
+            backends.load(six.text_type(p))
+
+    def test_trailing_dot(self, mocker):
+        """framework.backends.load: 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,
+            )})
+        with pytest.raises(backends.BackendNotImplementedError):
+            backends.load('foo.test_backend..gz')
+
+    def test_old(self, tmpdir, mock_backend):  # pylint: disable=unused-argument,redefined-outer-name
+        """backends.load: 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'.
+        """
+        tmpdir.mkdir('test')
+        p = tmpdir.join('test')
+        with p.join('results.test_backend.old').open('w') as f:
+            f.write('foo')
+
+        with pytest.raises(backends.BackendError):
+            backends.load(six.text_type(p))
+
+
+class TestSetMeta(object):
+    """Tests for the set_meta function."""
+
+    def test_basic(self, mocker):
+        """Basic argument/output test."""
+        mocker.patch.dict(
+            backends.BACKENDS,
+            {'test_backend': backends.register.Registry(
+                extensions=['.test_backend'],
+                backend=None,
+                load=None,
+                meta=lambda x: x.append('bar'),
+            )})
+
+        test = []
+        backends.set_meta('test_backend', test)
+        assert test == ['bar']
+
+    def test_no_backened(self):
+        """backends.set_meta: raises an error if there is no meta function."""
+        with pytest.raises(backends.BackendError):
+            backends.set_meta('foo', {})
+
+    def test_notimplemented(self, mock_backend):  # pylint: disable=redefined-outer-name,unused-argument
+        """backends.load(): An error is raised if a set_meta isn't properly
+        implmented.
+        """
+        with pytest.raises(backends.BackendNotImplementedError):
+            backends.set_meta('test_backend', {})
diff --git a/unittests/junit_backends_tests.py b/unittests/junit_backends_tests.py
index 5b0bead..4e5100d 100644
--- a/unittests/junit_backends_tests.py
+++ b/unittests/junit_backends_tests.py
@@ -35,8 +35,12 @@ import nose.tools as nt
 
 from framework import results, backends, grouptools, status
 from . import utils
-from .backends_tests import BACKEND_INITIAL_META
 
+BACKEND_INITIAL_META = {
+    'name': 'name',
+    'test_count': 0,
+    'env': {},
+}
 
 JUNIT_SCHEMA = os.path.join(os.path.dirname(__file__), 'schema', 'junit-7.xsd')
 
-- 
2.9.0



More information about the Piglit mailing list