[Piglit] [PATCH 5/6] framework tests: replace ugly generators with classes

Dylan Baker baker.dylan.c at gmail.com
Tue Sep 30 11:31:38 PDT 2014


Test generators solve some problems very nicely, namely that one wants
to use the same test 10 times with 10 different sets of inputs. What
they don't solve nicely is the problem of 10 different tests with 1 set
of inputs. This problem is solved better by a test class, with shared
data and methods that access that data.

This patch replaces 3 such generators with classes.

Signed-off-by: Dylan Baker <dylanx.c.baker at intel.com>
---
 framework/tests/profile_tests.py | 155 +++++++++++++++------------------------
 framework/tests/summary_tests.py | 119 ++++++++++++------------------
 2 files changed, 106 insertions(+), 168 deletions(-)

diff --git a/framework/tests/profile_tests.py b/framework/tests/profile_tests.py
index 1d825c7..73593ad 100644
--- a/framework/tests/profile_tests.py
+++ b/framework/tests/profile_tests.py
@@ -140,129 +140,94 @@ def test_testprofile_update_test_list():
     nt.assert_dict_equal(profile1.test_list, baseline)
 
 
- at utils.nose_generator
-def generate_prepare_test_list_flatten():
-    """ Generate tests for TestProfile.prepare_test_list() """
+class TestPrepareTestListFlatten(object):
+    """ tests for TestProfile.prepare_test_list() flatten """
     tests = {'group1': {'test1': 'thingy', 'group3': {'test2': 'thing'}},
              'group3': {'test5': 'other'}}
-    test_list = {'group1/test1': 'thingy', 'group1/group3/test2': 'thing',
-                 'group3/test5': 'other'}
+    testlist = {'group1/test1': 'thingy', 'group1/group3/test2': 'thing',
+                'group3/test5': 'other'}
 
-    check_flatten.description = \
-        "TestProfile.prepare_test_list flattens TestProfile.tests"
-    yield check_flatten, tests, test_list
+    def test_flatten_tests(self):
+        """ TestProfile.prepare_test_list flattens TestProfile.tests """
+        profile_ = profile.TestProfile()
+        profile_.tests = self.tests
+        profile_._flatten_group_hierarchy()
 
-    check_mixed_flatten.description = \
-        "TestProfile flattening is correct when tess and test_list are set"
-    yield check_mixed_flatten, tests, test_list
+        nt.assert_dict_equal(profile_.test_list, self.testlist)
 
+    def test_flatten_tests_into(self):
+        """ TestProfile.prepare_test_list merges tests and test_list """
+        profile_ = profile.TestProfile()
+        profile_.tests = self.tests
+        profile_.test_list['test8'] = 'other'
+        profile_._flatten_group_hierarchy()
 
- at nt.nottest
-def check_flatten(tests, testlist):
-    """ TestProfile.prepare_test_list flattens TestProfile.tests """
-    profile_ = profile.TestProfile()
-    profile_.tests = tests
-    profile_._flatten_group_hierarchy()
-
-    nt.assert_dict_equal(profile_.test_list, testlist)
-
-
- at nt.nottest
-def check_mixed_flatten(tests, testlist):
-    """ flattening is correct when tests and test_list are defined """
-    profile_ = profile.TestProfile()
-    profile_.tests = tests
-    profile_.test_list['test8'] = 'other'
-    profile_._flatten_group_hierarchy()
-
-    baseline = {'test8': 'other'}
-    baseline.update(testlist)
+        baseline = {'test8': 'other'}
+        baseline.update(self.testlist)
 
-    nt.assert_dict_equal(profile_.test_list, baseline)
+        nt.assert_dict_equal(profile_.test_list, baseline)
 
 
- at utils.nose_generator
-def generate_prepare_test_list_test_test_matches():
+class TestPrepareTestListMatches(object):
     """ Generate tests for TestProfile.perpare_test_list filtering """
     data = {'group1/test1': 'thingy', 'group1/group3/test2': 'thing',
             'group3/test5': 'other'}
 
-    test_matches_filter_mar_1.description = (
-        "TestProfile.prepare_test_list: "
-        "'not env.filter or matches_any_regex() env.filter is False")
-    yield test_matches_filter_mar_1, data
+    def test_matches_filter_1(self):
+        """TestProfile._prepare_test_list: not env.filter or matches_any_regex()
+        env.filter is False
 
-    test_matches_filter_mar_2.description = (
-        "TestProfile.prepare_test_list: "
-        "Tests 'not env.filter or matches_any_regex() mar is False")
-    yield test_matches_filter_mar_2, data
+        Nothing should be filtered
 
-    test_matches_env_exclude.description = (
-        "TestProfile.prepare_test_list: "
-        "Tests 'not path in env.exclude_tests' is True")
-    yield test_matches_env_exclude, data
+        """
+        env = core.Options()
 
-    test_matches_exclude_mar.description = \
-        "TestProfile.prepare_test_list: Tests 'not matches_any_regex"
-    yield test_matches_exclude_mar, data
+        profile_ = profile.TestProfile()
+        profile_.test_list = self.data
+        profile_._prepare_test_list(env)
 
+        nt.assert_dict_equal(profile_.test_list, self.data)
 
- at nt.nottest
-def test_matches_filter_mar_1(data):
-    """ Tests 'not env.filter or matches_any_regex() env.filter is False
+    def test_matches_filter_2(self):
+        """TestProfile.prepare_test_list: not env.filter or matches_any_regex()
+        mar is False
 
-    Nothing should be filtered.
+        """
+        env = core.Options(include_filter=['test5'])
 
-    """
-    env = core.Options()
+        profile_ = profile.TestProfile()
+        profile_.test_list = self.data
+        profile_._prepare_test_list(env)
 
-    profile_ = profile.TestProfile()
-    profile_.test_list = data
-    profile_._prepare_test_list(env)
+        baseline = {'group3/test5': 'other'}
 
-    nt.assert_dict_equal(profile_.test_list, data)
+        nt.assert_dict_equal(profile_.test_list, baseline)
 
+    def test_env_exclude_matches(self):
+        """TestProfile.prepare_test_list: not path in env.exclude_tests is True
 
- at nt.nottest
-def test_matches_filter_mar_2(data):
-    """ Tests 'not env.filter or matches_any_regex() mar is False"""
-    env = core.Options(include_filter=['test5'])
+        """
+        env = core.Options()
+        env.exclude_tests.add('group3/test5')
 
-    profile_ = profile.TestProfile()
-    profile_.test_list = data
-    profile_._prepare_test_list(env)
+        profile_ = profile.TestProfile()
+        profile_.test_list = self.data
+        profile_._prepare_test_list(env)
 
-    baseline = {'group3/test5': 'other'}
+        baseline = copy.deepcopy(self.data)
+        del baseline['group3/test5']
 
-    nt.assert_dict_equal(profile_.test_list, baseline)
-
-
- at nt.nottest
-def test_matches_env_exclude(data):
-    """ Tests 'not path in env.exclude_tests  """
-    env = core.Options()
-    env.exclude_tests.add('group3/test5')
+        nt.assert_dict_equal(profile_.test_list, baseline)
 
-    profile_ = profile.TestProfile()
-    profile_.test_list = data
-    profile_._prepare_test_list(env)
-
-    baseline = copy.deepcopy(data)
-    del baseline['group3/test5']
+    def test_exclude_matches(self):
+        """TestProfile.prepare_test_list Tests not matches_any_regex"""
+        env = core.Options(exclude_filter=['test5'])
 
-    nt.assert_dict_equal(profile_.test_list, baseline)
+        profile_ = profile.TestProfile()
+        profile_.test_list = self.data
+        profile_._prepare_test_list(env)
 
+        baseline = copy.deepcopy(self.data)
+        del baseline['group3/test5']
 
- at nt.nottest
-def test_matches_exclude_mar(data):
-    """ Tests 'not matches_any_regexp() """
-    env = core.Options(exclude_filter=['test5'])
-
-    profile_ = profile.TestProfile()
-    profile_.test_list = data
-    profile_._prepare_test_list(env)
-
-    baseline = copy.deepcopy(data)
-    del baseline['group3/test5']
-
-    nt.assert_dict_equal(profile_.test_list, baseline)
+        nt.assert_dict_equal(profile_.test_list, baseline)
diff --git a/framework/tests/summary_tests.py b/framework/tests/summary_tests.py
index 2f34e55..9152866 100644
--- a/framework/tests/summary_tests.py
+++ b/framework/tests/summary_tests.py
@@ -76,76 +76,49 @@ def check_sets(old, ostat, new, nstat, set_):
                             msg="{0} was not appended".format(set_))
 
 
- at utils.nose_generator
-def test_subtest_handling():
-    data = copy.deepcopy(utils.JSON_DATA)
-    data['tests']['with_subtests'] = {}
-    data['tests']['with_subtests']['result'] = 'pass'
-
-    data['tests']['with_subtests']['subtest'] = {}
-    data['tests']['with_subtests']['subtest']['subtest1'] = 'fail'
-    data['tests']['with_subtests']['subtest']['subtest2'] = 'warn'
-    data['tests']['with_subtests']['subtest']['subtest3'] = 'crash'
-    data['tests']['is_skip'] = {}
-    data['tests']['is_skip']['result'] = 'skip'
-
-    with utils.with_tempfile(json.dumps(data)) as sumfile:
-        summ = summary.Summary([sumfile])
-
-        check_subtests_are_tests.description = \
-            "Subtests should be treated as full tests "
-        yield check_subtests_are_tests, summ
-
-        check_tests_w_subtests_are_groups.description = \
-            "Tests with subtests should be a group"
-        yield check_tests_w_subtests_are_groups, summ
-
-        test_removed_from_all.description = \
-            "Tests with subtests should not be in the tests['all'] name"
-        yield test_removed_from_all, summ
-
-        subtest_not_skip_notrun.description = \
-            "Skip's should not become NotRun"
-        yield subtest_not_skip_notrun, summ
-
-
- at nt.nottest
-def check_subtests_are_tests(summary_):
-    """ Subtests should be treated as full tests """
-    print(summary_.fractions)
-    nt.assert_equal(summary_.fractions['fake-tests']['with_subtests'], (0, 3),
-        msg="Summary.fraction['fake-tests']['with_subtests'] should "
-            "be (0, 3), but isn't")
-
-
- at nt.nottest
-def check_tests_w_subtests_are_groups(summary_):
-    """ Tests with subtests should be a group
-
-    We know that the status will be 'pass' if it's not being overwritten, and
-    will be 'crash' if it has. (since we set the data that way)
-
-    """
-    print(summary_.status)
-    nt.assert_equal(
-        str(summary_.status['fake-tests']['with_subtests']), 'crash',
-        msg="Summary.status['fake-tests']['with_subtests'] should "
-            "be crash, but isn't")
-
-
- at nt.nottest
-def test_removed_from_all(summary_):
-    """ Tests with subtests should not be in the all results """
-    print(summary_.tests['all'])
-    nt.assert_not_in('with_subtests', summary_.tests['all'],
-        msg="Test with subtests should have been removed from "
-            "self.tests['all'], but wasn't")
-
-
- at nt.nottest
-def subtest_not_skip_notrun(summary_):
-    """ Ensure that skips are not changed to notruns """
-    print(summary_.status['fake-tests']['is_skip'])
-    print(summary_.results[0].tests['is_skip'])
-    nt.eq_(summary_.status['fake-tests']['is_skip'], 'skip',
-        msg="Status should be skip but was changed")
+class TestSubtestHandling(object):
+    @classmethod
+    def setup_class(cls):
+        data = copy.deepcopy(utils.JSON_DATA)
+        data['tests']['with_subtests'] = {}
+        data['tests']['with_subtests']['result'] = 'pass'
+
+        data['tests']['with_subtests']['subtest'] = {}
+        data['tests']['with_subtests']['subtest']['subtest1'] = 'fail'
+        data['tests']['with_subtests']['subtest']['subtest2'] = 'warn'
+        data['tests']['with_subtests']['subtest']['subtest3'] = 'crash'
+        data['tests']['is_skip'] = {}
+        data['tests']['is_skip']['result'] = 'skip'
+
+        with utils.with_tempfile(json.dumps(data)) as sumfile:
+            cls.summ = summary.Summary([sumfile])
+
+    def test_subtests_are_tests(self):
+        """ summary.Summary: subtests should be treated as full tests """
+        nt.assert_equal(
+            self.summ.fractions['fake-tests']['with_subtests'], (0, 3),
+            msg="Summary.fraction['fake-tests']['with_subtests'] should "
+                "be (0, 3), but isn't")
+
+    def test_tests_w_subtests_are_groups(self):
+        """ summary.Summary: Tests with subtests should be a group
+
+        We know that the status will be 'pass' if it's not being overwritten,
+        and will be 'crash' if it has. (since we set the data that way)
+
+        """
+        nt.assert_equal(
+            str(self.summ.status['fake-tests']['with_subtests']), 'crash',
+            msg="Summary.status['fake-tests']['with_subtests'] should "
+                "be crash, but isn't")
+
+    def test_removed_from_all(self):
+        """ Tests with subtests should not be in the all results """
+        nt.assert_not_in('with_subtests', self.summ.tests['all'],
+                         msg="Test with subtests should have been removed from"
+                             " self.tests['all'], but wasn't")
+
+    def test_subtest_not_skip_notrun(self):
+        """ Ensure that skips are not changed to notruns """
+        nt.eq_(self.summ.status['fake-tests']['is_skip'], 'skip',
+               msg="Status should be skip but was changed")
-- 
2.1.1



More information about the Piglit mailing list