From a795b87753b201d9cb4aedfec454098e22553600 Mon Sep 17 00:00:00 2001 From: Lucas Meneghel Rodrigues Date: Thu, 11 Jun 2015 21:00:01 -0300 Subject: [PATCH] avocado.core.test: Fix problem with unittest compatibility Introduced with commit d6abdcd4a6ea382c0447ece49f983b37823de245, avocado tests no longer report correctly to the unittest runner. The reason is that there is a structure that must be correctly updated by the run() method (the result instance), that was not being updated in the new implementation. Therefore, one possible fix, presented here, is to once again *not* implement run() at all. this way we avoid messing around with internal data structures, and when avocado is executed under nosetests, or the default unittest runner, we get the expected behavior of the method. The downside is that we have less perceived integration among both classes. Signed-off-by: Lucas Meneghel Rodrigues --- avocado/core/test.py | 21 ++++--------------- examples/tests/skiponsetup.py | 6 ++++++ .../all/unit/avocado/jsonresult_unittest.py | 8 ++++++- selftests/all/unit/avocado/test_unittest.py | 15 ------------- selftests/all/unit/avocado/xunit_unittest.py | 8 ++++++- 5 files changed, 24 insertions(+), 34 deletions(-) diff --git a/avocado/core/test.py b/avocado/core/test.py index 894997c3..99bdc165 100644 --- a/avocado/core/test.py +++ b/avocado/core/test.py @@ -300,17 +300,6 @@ class Test(unittest.TestCase): """ pass - def runTest(self): - """ - Actual test payload. Must be implemented by tests. - - In case of an existing test suite wrapper, it'll execute the suite, - or perform a series of operations, and based in the results of the - operations decide if the test pass (let the test complete) or fail - (raise a test related exception). - """ - pass - def tearDown(self): """ Cleanup stage after the runTest is done. @@ -345,11 +334,9 @@ class Test(unittest.TestCase): 'Actual:\n%s\nExpected:\n%s' % (actual, expected)) self.assertEqual(expected, actual, msg) - def run(self, result=None): + def _run_avocado(self): """ - Run test method, for compatibility with unittest.TestCase. - - :result: Unused param, compatibiltiy with :class:`unittest.TestCase`. + Auxiliary method to run_avocado. """ testMethod = getattr(self, self._testMethodName) self.start_logging() @@ -437,7 +424,7 @@ class Test(unittest.TestCase): os.environ['AVOCADO_TEST_OUTPUTDIR'] = self.outputdir os.environ['AVOCADO_TEST_SYSINFODIR'] = self.sysinfodir - def run_avocado(self, result=None): + def run_avocado(self): """ Wraps the run method, for execution inside the avocado runner. @@ -446,7 +433,7 @@ class Test(unittest.TestCase): self._setup_environment_variables() try: self.tag_start() - self.run(result) + self._run_avocado() except exceptions.TestBaseException, detail: self.status = detail.status self.fail_class = detail.__class__.__name__ diff --git a/examples/tests/skiponsetup.py b/examples/tests/skiponsetup.py index ae8b8e04..1067f44f 100644 --- a/examples/tests/skiponsetup.py +++ b/examples/tests/skiponsetup.py @@ -16,5 +16,11 @@ class SkipOnSetupTest(Test): """ self.skip('This should end with SKIP.') + def test_wont_be_executed(self): + """ + This won't get to be executed, given that setUp calls .skip(). + """ + pass + if __name__ == "__main__": main() diff --git a/selftests/all/unit/avocado/jsonresult_unittest.py b/selftests/all/unit/avocado/jsonresult_unittest.py index 29d12c29..2186a183 100644 --- a/selftests/all/unit/avocado/jsonresult_unittest.py +++ b/selftests/all/unit/avocado/jsonresult_unittest.py @@ -41,6 +41,12 @@ class _Stream(object): class JSONResultTest(unittest.TestCase): def setUp(self): + + class SimpleTest(Test): + + def runTest(self): + pass + self.tmpfile = tempfile.mkstemp() self.tmpdir = tempfile.mkdtemp() args = argparse.Namespace(json_output=self.tmpfile[1]) @@ -49,7 +55,7 @@ class JSONResultTest(unittest.TestCase): self.test_result = jsonresult.JSONTestResult(stream, args) self.test_result.filename = self.tmpfile[1] self.test_result.start_tests() - self.test1 = Test(job=job.Job(), base_logdir=self.tmpdir) + self.test1 = SimpleTest(job=job.Job(), base_logdir=self.tmpdir) self.test1.status = 'PASS' self.test1.time_elapsed = 1.23 diff --git a/selftests/all/unit/avocado/test_unittest.py b/selftests/all/unit/avocado/test_unittest.py index 897e157a..172af868 100644 --- a/selftests/all/unit/avocado/test_unittest.py +++ b/selftests/all/unit/avocado/test_unittest.py @@ -36,26 +36,11 @@ class TestClassTest(unittest.TestCase): self.assertTrue(variable) self.whiteboard = 'foo' - class EmptyTest(test.Test): - - """ - I don't have runTest() defined! - """ - pass - self.base_logdir = tempfile.mkdtemp(prefix='avocado_test_unittest') self.tst_instance_pass = AvocadoPass(base_logdir=self.base_logdir) self.tst_instance_pass.run_avocado() self.tst_instance_pass_new = AvocadoPass(base_logdir=self.base_logdir) self.tst_instance_pass_new.run_avocado() - self.tst_instance_empty = EmptyTest(base_logdir=self.base_logdir) - self.tst_instance_empty.run_avocado() - - def testRunTest(self): - self.assertEqual(self.tst_instance_empty.runTest(), None) - - def testRunAvocado(self): - self.assertEqual(self.tst_instance_empty.status, 'PASS') def testClassAttributesName(self): self.assertEqual(self.tst_instance_pass.name, 'AvocadoPass') diff --git a/selftests/all/unit/avocado/xunit_unittest.py b/selftests/all/unit/avocado/xunit_unittest.py index a1476dc9..79f52fa3 100644 --- a/selftests/all/unit/avocado/xunit_unittest.py +++ b/selftests/all/unit/avocado/xunit_unittest.py @@ -45,13 +45,19 @@ class _Stream(object): class xUnitSucceedTest(unittest.TestCase): def setUp(self): + + class SimpleTest(Test): + + def runTest(self): + pass + self.tmpfile = tempfile.mkstemp() self.tmpdir = tempfile.mkdtemp() args = argparse.Namespace() args.xunit_output = self.tmpfile[1] self.test_result = xunit.xUnitTestResult(stream=_Stream(), args=args) self.test_result.start_tests() - self.test1 = Test(job=job.Job(), base_logdir=self.tmpdir) + self.test1 = SimpleTest(job=job.Job(), base_logdir=self.tmpdir) self.test1.status = 'PASS' self.test1.time_elapsed = 1.23 -- GitLab