[Piglit] [PATCH 2/6] framework tests: replace assert keyword with nose.tools wrappers

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


These wrappers have the nice property of generating a nice, generally
useful error message when the assertion fails, unlike 'assert' which
requires that the code writer add such a message. This message is almost
always missing.

Signed-off-by: Dylan Baker <dylanx.c.baker at intel.com>
---
 framework/tests/core_tests.py       | 43 ++++++++++++++++---------------------
 framework/tests/exectest_test.py    |  4 ++--
 framework/tests/gleantest_tests.py  |  6 ++++--
 framework/tests/log_tests.py        |  7 +++---
 framework/tests/profile_tests.py    |  8 +++----
 framework/tests/results_tests.py    |  7 +++---
 framework/tests/results_v0_tests.py | 26 +++++++++++-----------
 framework/tests/status_tests.py     | 14 ++++++------
 8 files changed, 58 insertions(+), 57 deletions(-)

diff --git a/framework/tests/core_tests.py b/framework/tests/core_tests.py
index eb4d8fe..b44bb83 100644
--- a/framework/tests/core_tests.py
+++ b/framework/tests/core_tests.py
@@ -65,35 +65,30 @@ def test_parse_listfile_return():
     with utils.with_tempfile(contents) as tfile:
         results = core.parse_listfile(tfile)
 
-    assert isinstance(results, collections.Container)
+    nt.assert_is_instance(results, collections.Container)
 
 
-def check_whitespace(actual, base, message):
-    """ check that the string has not trailing whitespace """
-    assert base == actual, message
+class TestListFileWhitespace(object):
+    """ Tests for parse_listfile's whitespace handling """
+    @classmethod
+    def setup_class(cls):
+        """ Test that parse_listfile remove whitespace """
+        contents = "/tmp/foo\n/tmp/foo  \n/tmp/foo\t\n"
 
+        with utils.with_tempfile(contents) as tfile:
+            cls.results = core.parse_listfile(tfile)
 
-def test_parse_listfile_whitespace():
-    """ Test that parse_listfile remove whitespace """
-    contents = "/tmp/foo\n/tmp/foo  \n/tmp/foo\t\n"
+    def test_newline(self):
+        """ core.parse_listfile removes trailing newlines """
+        nt.assert_equal(self.results[0], "/tmp/foo")
 
-    with utils.with_tempfile(contents) as tfile:
-        results = core.parse_listfile(tfile)
-
-    yld = check_whitespace
-
-    # Test for newlines
-    yld.description = ("Test that trailing newlines are removed by "
-                       "parse_listfile")
-    yield yld, results[0], "/tmp/foo", "Trailing newline not removed!"
-
-    # test for normal spaces
-    yld.description = "Test that trailing spaces are removed by parse_listfile"
-    yield yld, results[1], "/tmp/foo", "Trailing spaces not removed!"
+    def test_space(self):
+        """ core.parse_listfile removes trailing spaces """
+        nt.assert_equal(self.results[1], "/tmp/foo")
 
-    # test for tabs
-    yld.description = "Test that trailing tabs are removed by parse_listfile"
-    yield yld, results[2], "/tmp/foo", "Trailing tab not removed!"
+    def test_tabs(self):
+        """ core.parse_listfile removes trailing tabs """
+        nt.assert_equal(self.results[2], "/tmp/foo")
 
 
 def test_parse_listfile_tilde():
@@ -110,7 +105,7 @@ def test_parse_listfile_tilde():
     with utils.with_tempfile(contents) as tfile:
         results = core.parse_listfile(tfile)
 
-    assert results[0] == os.path.expandvars("$HOME/foo")
+    nt.assert_equal(results[0], os.path.expandvars("$HOME/foo"))
 
 
 class TestGetConfig(utils.TestWithEnvClean):
diff --git a/framework/tests/exectest_test.py b/framework/tests/exectest_test.py
index 113f9ae..3a639d2 100644
--- a/framework/tests/exectest_test.py
+++ b/framework/tests/exectest_test.py
@@ -89,7 +89,7 @@ def test_piglittest_interpret_result():
     test = PiglitTest('foo')
     test.result['out'] = 'PIGLIT: {"result": "pass"}\n'
     test.interpret_result()
-    assert test.result['result'] == 'pass'
+    nt.assert_equal(test.result['result'], 'pass')
 
 
 def test_piglittest_interpret_result_subtest():
@@ -98,7 +98,7 @@ def test_piglittest_interpret_result_subtest():
     test.result['out'] = ('PIGLIT: {"result": "pass"}\n'
                           'PIGLIT: {"subtest": {"subtest": "pass"}}\n')
     test.interpret_result()
-    assert test.result['subtest']['subtest'] == 'pass'
+    nt.assert_equal(test.result['subtest']['subtest'],'pass')
 
 
 def test_piglitest_no_clobber():
diff --git a/framework/tests/gleantest_tests.py b/framework/tests/gleantest_tests.py
index 772dfd7..ccc2fbb 100644
--- a/framework/tests/gleantest_tests.py
+++ b/framework/tests/gleantest_tests.py
@@ -23,6 +23,7 @@
 from __future__ import print_function
 import os
 from nose.plugins.skip import SkipTest
+import nose.tools as nt
 
 import framework.tests.utils as utils
 from framework.gleantest import GleanTest
@@ -49,7 +50,7 @@ def test_GLOBAL_PARAMS_assignment():
     test1 = GleanTest('basic')
     GleanTest.GLOBAL_PARAMS = ['--quick']
     test2 = GleanTest('basic')
-    assert test1.command == test2.command
+    nt.assert_equal(test1.command, test2.command)
 
 
 def test_bad_returncode():
@@ -63,4 +64,5 @@ def test_bad_returncode():
     test = GleanTest('basic')
     test.result['returncode'] = 1
     test.interpret_result()
-    assert test.result['result'] == 'fail', "Result should have been fail"
+    nt.assert_equal(test.result['result'], 'fail',
+                    msg="Result should have been fail")
diff --git a/framework/tests/log_tests.py b/framework/tests/log_tests.py
index 85f5ce5..d66a1a4 100644
--- a/framework/tests/log_tests.py
+++ b/framework/tests/log_tests.py
@@ -47,7 +47,7 @@ def test_log_factory_returns_log():
     """ LogManager.get() returns a BaseLog derived instance """
     logger = log.LogManager('quiet', 100)
     log_inst = logger.get()
-    assert isinstance(log_inst, log.BaseLog)
+    nt.assert_is_instance(log_inst, log.BaseLog)
 
 
 @utils.nose_generator
@@ -85,7 +85,7 @@ def check_for_output(func, args, file_=sys.stdout):
     func(*args)
     # In nose sys.stdout and sys.stderr is a StringIO object, it returns a
     # string of everything after the tell.
-    assert file_.read() == ''
+    nt.assert_equal(file_.read(), '')
 
 
 @utils.nose_generator
@@ -134,7 +134,8 @@ def check_no_output(func, args, file_=sys.stdout):
 
     func(*args)
     file_.seek(-1)
-    assert file_.tell() == 0, 'file.tell() is at {}'.format(file_.tell())
+    nt.assert_equal(file_.tell(), 0,
+                    msg='file.tell() is at {}'.format(file_.tell()))
 
 
 @utils.nose_generator
diff --git a/framework/tests/profile_tests.py b/framework/tests/profile_tests.py
index 5afc928..83880f9 100644
--- a/framework/tests/profile_tests.py
+++ b/framework/tests/profile_tests.py
@@ -50,13 +50,13 @@ def test_load_test_profile_no_profile():
 def test_load_test_profile_returns():
     """ load_test_profile returns a TestProfile instance """
     profile_ = profile.load_test_profile('sanity')
-    assert isinstance(profile_, profile.TestProfile)
+    nt.assert_is_instance(profile_, profile.TestProfile)
 
 
 def test_testprofile_default_dmesg():
     """ Dmesg initializes false """
     profile_ = profile.TestProfile()
-    assert isinstance(profile_.dmesg, dmesg.DummyDmesg)
+    nt.assert_is_instance(profile_.dmesg, dmesg.DummyDmesg)
 
 
 def test_testprofile_set_dmesg_true():
@@ -65,7 +65,7 @@ def test_testprofile_set_dmesg_true():
         raise SkipTest('No dmesg support on this platform')
     profile_ = profile.TestProfile()
     profile_.dmesg = True
-    assert isinstance(profile_.dmesg, dmesg.LinuxDmesg)
+    nt.assert_is_instance(profile_.dmesg, dmesg.LinuxDmesg)
 
 
 def test_testprofile_set_dmesg_false():
@@ -75,7 +75,7 @@ def test_testprofile_set_dmesg_false():
     profile_ = profile.TestProfile()
     profile_.dmesg = True
     profile_.dmesg = False
-    assert isinstance(profile_.dmesg, dmesg.DummyDmesg)
+    nt.assert_is_instance(profile_.dmesg, dmesg.DummyDmesg)
 
 
 def test_testprofile_flatten():
diff --git a/framework/tests/results_tests.py b/framework/tests/results_tests.py
index f9c1ce2..449ccb2 100644
--- a/framework/tests/results_tests.py
+++ b/framework/tests/results_tests.py
@@ -91,8 +91,9 @@ def test_testresult_to_status():
     """ TestResult initialized with result key converts the value to a Status
     """
     result = results.TestResult({'result': 'pass'})
-    assert isinstance(result['result'], status.Status), \
-        "Result key not converted to a status object"
+    nt.assert_is_instance(
+        result['result'], status.Status,
+        msg="Result key not converted to a status object")
 
 
 def test_testrunresult_write():
@@ -219,7 +220,7 @@ class TestJUnitSingleTest(TestJunitNoTests):
         """ JUnitBackend.write_test() (once) produces valid xml """
         schema = etree.XMLSchema(file=JUNIT_SCHEMA)
         with open(self.test_file, 'r') as f:
-            assert schema.validate(etree.parse(f)), 'xml is not valid'
+            nt.ok_(schema.validate(etree.parse(f)), msg='xml is not valid')
 
 
 class TestJUnitMultiTest(TestJUnitSingleTest):
diff --git a/framework/tests/results_v0_tests.py b/framework/tests/results_v0_tests.py
index fd8be26..2f465d9 100644
--- a/framework/tests/results_v0_tests.py
+++ b/framework/tests/results_v0_tests.py
@@ -106,23 +106,23 @@ with utils.with_tempfile(json.dumps(DATA)) as f:
 
 def test_dmesg():
     """ version 1: dmesg is converted from a list to a string """
-    assert RESULT.tests['sometest']['dmesg'] == 'this\nis\ndmesg'
+    nt.assert_equal(RESULT.tests['sometest']['dmesg'], 'this\nis\ndmesg')
 
 
 def test_subtests_remove_duplicates():
     """ Version 1: Removes duplicate entries """
-    assert 'group1/groupA/test/subtest 1' not in RESULT.tests
-    assert 'group1/groupA/test/subtest 2' not in RESULT.tests
+    nt.assert_not_in('group1/groupA/test/subtest 1', RESULT.tests)
+    nt.assert_not_in('group1/groupA/test/subtest 2', RESULT.tests)
 
 
 def test_subtests_add_test():
     """ Version 1: Add an entry for the actual test """
-    assert RESULT.tests.get('group1/groupA/test')
+    nt.ok_(RESULT.tests.get('group1/groupA/test'))
 
 
 def test_subtests_test_is_testresult():
     """ Version 1: The result of the new test is a TestResult Instance """
-    assert isinstance(
+    nt.assert_is_instance(
         RESULT.tests['group1/groupA/test'],
         results.TestResult)
 
@@ -130,12 +130,12 @@ def test_subtests_test_is_testresult():
 def test_info_delete():
     """ Version 1: Remove the info name from results """
     for value in RESULT.tests.itervalues():
-        assert 'info' not in value
+        nt.assert_not_in('info', value)
 
 
 def test_returncode_from_info():
     """ Version 1: Use the returncode from info if there is no returncode """
-    assert RESULT.tests['sometest']['returncode'] == 1
+    nt.assert_equal(RESULT.tests['sometest']['returncode'], 1)
 
 
 def test_returncode_no_override():
@@ -146,22 +146,22 @@ def test_returncode_no_override():
     there is a value in returncode already
 
     """
-    assert RESULT.tests['group1/groupA/test']['returncode'] != 1
+    nt.assert_not_equal(RESULT.tests['group1/groupA/test']['returncode'], 1)
 
 
 def test_err_from_info():
     """ Version 1: add an err attribute from info """
-    assert RESULT.tests['group1/groupA/test']['err'] == 'stderr'
+    nt.assert_equal(RESULT.tests['group1/groupA/test']['err'], 'stderr')
 
 
 def test_out_from_info():
     """ Version 1: add an out attribute from info """
-    assert RESULT.tests['group1/groupA/test']['out'] == 'stdout'
+    nt.assert_equal(RESULT.tests['group1/groupA/test']['out'], 'stdout')
 
 
 def test_set_version():
     """ Version 1: Set the version to 1 """
-    assert RESULT.results_version == 1
+    nt.assert_equal(RESULT.results_version, 1)
 
 
 def test_dont_break_single_subtest():
@@ -179,7 +179,7 @@ def test_dont_break_single_subtest():
     should remain test/foo/bar since bar is the name of the test not a subtest
 
     """
-    assert RESULT.tests['single/test/thing']
+    nt.ok_(RESULT.tests['single/test/thing'])
 
 
 def test_info_split():
@@ -203,4 +203,4 @@ def test_subtests_with_slash():
 
 def test_handle_fixed_subtests():
     """ Version 1: Correctly handle new single entry subtests correctly """
-    assert 'group3/groupA/test' in RESULT.tests.iterkeys()
+    nt.assert_in('group3/groupA/test', RESULT.tests)
diff --git a/framework/tests/status_tests.py b/framework/tests/status_tests.py
index 63f7ab9..81423b1 100644
--- a/framework/tests/status_tests.py
+++ b/framework/tests/status_tests.py
@@ -66,8 +66,10 @@ def compare_status_nochangestatus():
 
 def check_lookup(stat):
     """ Lookup a status """
-    stt = status.status_lookup(stat)
-    assert stt
+    try:
+        status.status_lookup(stat)
+    except status.StatusException as e:
+        raise AssertionError(e)
 
 
 @utils.nose_generator
@@ -89,22 +91,22 @@ def test_status_in():
     stat = status.PASS
     slist = ['pass']
 
-    assert stat in slist
+    nt.assert_in(stat, slist)
 
 
 def is_regression(new, old):
     """ Test that old -> new is a regression """
-    assert status.status_lookup(new) < status.status_lookup(old)
+    nt.assert_less(status.status_lookup(new), status.status_lookup(old))
 
 
 def is_fix(new, old):
     """ Test that new -> old is a fix """
-    assert status.status_lookup(new) > status.status_lookup(old)
+    nt.assert_greater(status.status_lookup(new), status.status_lookup(old))
 
 
 def is_not_equivalent(new, old):
     """ Test that new != old """
-    assert status.status_lookup(new) != status.status_lookup(old)
+    nt.assert_not_equal(status.status_lookup(new), status.status_lookup(old))
 
 
 @utils.nose_generator
-- 
2.1.1



More information about the Piglit mailing list