[Piglit] [PATCH 31/49] unittests: port summary_console_tests to pytest

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


Signed-off-by: Dylan Baker <dylanx.c.baker at intel.com>
---
 .../summary/test_console.py}                       | 135 +++++++++------------
 1 file changed, 55 insertions(+), 80 deletions(-)
 rename unittests/{summary_console_tests.py => framework/summary/test_console.py} (56%)

diff --git a/unittests/summary_console_tests.py b/unittests/framework/summary/test_console.py
similarity index 56%
rename from unittests/summary_console_tests.py
rename to unittests/framework/summary/test_console.py
index be39f7e..0f7b067 100644
--- a/unittests/summary_console_tests.py
+++ b/unittests/framework/summary/test_console.py
@@ -1,4 +1,4 @@
-# Copyright (c) 2015 Intel Corporation
+# Copyright (c) 2015-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
@@ -18,73 +18,58 @@
 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 # SOFTWARE.
 
-"""Tests for framework.summary.console_
+"""Tests for framework.summary.console_.
 
 Some of these tests are fickle, since we're testing the output of a summary
 generator.
-
 """
 
-# pylint: disable=protected-access,invalid-name
-
 from __future__ import (
     absolute_import, division, print_function, unicode_literals
 )
-import sys
 
-import nose.tools as nt
+import pytest
 import six
-from six.moves import cStringIO as StringIO
 
-from . import utils
-from framework import results, grouptools
+from framework import results
+from framework import grouptools
 from framework.summary import console_, common
 
-
-def get_stdout(callable_):
-    """Capture stdout from a import callable"""
-    capture = StringIO()
-    temp = sys.stdout
-    sys.stdout = capture
-    callable_()
-    sys.stdout = temp
-
-    return capture.getvalue()
-
-
-class Test_print_summary(object):
+# pylint: disable=no-self-use,protected-access
+
+_ENUMS = {
+    1: 'names',
+    2: 'divider',
+    3: 'pass',
+    4: 'fail',
+    5: 'crash',
+    6: 'skip',
+    7: 'timeout_',  # "timeout" interferes with py.test's dependency injection
+    8: 'warn',
+    9: 'incomplete',
+    10: 'dmesg-warn',
+    11: 'dmesg-fail',
+    12: 'changes',
+    13: 'fixes',
+    14: 'regressions',
+    15: 'total',
+}
+
+class TestPrintSummary(object):
     """Tests for the console output."""
-    _ENUMS = {
-        'names': 1,
-        'dividier': 2,
-        'pass': 3,
-        'fail': 4,
-        'crash': 5,
-        'skip': 6,
-        'timeout': 7,
-        'warn': 8,
-        'incomplete': 9,
-        'dmesg-warn': 10,
-        'dmesg-fail': 11,
-        'changes': 12,
-        'fixes': 13,
-        'regressions': 14,
-        'total': 15,
-    }
-
-    @classmethod
-    def setup_class(cls):
+
+    @pytest.mark.parametrize("index", six.iterkeys(_ENUMS), ids=lambda i: _ENUMS[i])
+    def test_values(self, index, capsys):
         """Create both an expected value and an actual value.
 
         The expected value makes use of the template, this helps to minimize
         the number of changes that need to be made if the output is altered.
-
         """
         names = [grouptools.join('foo', 'bar', 'oink', 'foobar', 'boink'),
                  'foo', 'bar']
         template = '{: >20.20} {: >6.6}'
 
-        cls.expected = console_._SUMMARY_TEMPLATE.format(
+        expected = console_._SUMMARY_TEMPLATE.format(
             names=' '.join(['this is a really rea', 'a name']),
             divider=' '.join(['--------------------', '------']),
             pass_=template.format('1', '2'),
@@ -119,50 +104,40 @@ class Test_print_summary(object):
 
         reses = common.Results([res1, res2])
 
-        cls.actual = get_stdout(
-            lambda: console_._print_summary(reses)).split('\n')
-
-    @utils.nose.generator
-    def test_values(self):
-        """Generate a bunch of tests."""
-        def test(value):
-            nt.eq_(self.actual[value], self.expected[value],
-                   msg='Values do not match\n'
-                       'expected "{}"\n'
-                       'actual   "{}"'.format(
-                           self.expected[value], self.actual[value]))
+        console_._print_summary(reses)
+        actual = capsys.readouterr()[0].splitlines()
 
-        description = "summary.console_._print_summary: calculates {} correctly"
+        assert actual[index] == expected[index]
 
-        for key, value in six.iteritems(self._ENUMS):
-            test.description = description.format(key)
-            yield test, value
 
+class TestPrintResult(object):
+    """Tests for the _print_result function."""
 
-def test_print_result():
-    """summary.console_._print_result: prints expected values"""
-    res1 = results.TestrunResult()
-    res1.tests['foo'] = results.TestResult('pass')
-
-    res2 = results.TestrunResult()
-    res2.tests['foo'] = results.TestResult('fail')
+    def test_basic(self, capsys):
+        """summary.console_._print_result: prints expected values."""
+        res1 = results.TestrunResult()
+        res1.tests['foo'] = results.TestResult('pass')
 
-    reses = common.Results([res1, res2])
+        res2 = results.TestrunResult()
+        res2.tests['foo'] = results.TestResult('fail')
 
-    expected = 'foo: pass fail\n'
-    actual = get_stdout(lambda: console_._print_result(reses, reses.names.all))
+        reses = common.Results([res1, res2])
 
-    nt.eq_(expected, actual)
+        expected = 'foo: pass fail\n'
+        console_._print_result(reses, reses.names.all)
+        actual, _ = capsys.readouterr()
 
+        assert expected == actual
 
-def test_print_result_replaces():
-    """summary.console_._print_result: Replaces separtaor with /"""
-    res1 = results.TestrunResult()
-    res1.tests[grouptools.join('foo', 'bar')] = results.TestResult('pass')
+    def test_replaces_separator(self, capsys):
+        """summary.console_._print_result: Replaces separator with /."""
+        res1 = results.TestrunResult()
+        res1.tests[grouptools.join('foo', 'bar')] = results.TestResult('pass')
 
-    reses = common.Results([res1])
+        reses = common.Results([res1])
 
-    expected = 'foo/bar: pass\n'
-    actual = get_stdout(lambda: console_._print_result(reses, reses.names.all))
+        expected = 'foo/bar: pass\n'
+        console_._print_result(reses, reses.names.all)
+        actual, _ = capsys.readouterr()
 
-    nt.eq_(expected, actual)
+        assert expected == actual
-- 
2.9.0



More information about the Piglit mailing list