1 # -*- coding: utf-8 -*-
2 # leap.common.testing.test_basetest
3 # Copyright (C) 2013 LEAP
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 Unittests for base test
19 ...becase it's oh so meta
22 import unittest2 as unittest
29 from leap.testing.basetest import BaseLeapTest
31 _tempdir = None # global for tempdir checking
34 class _TestCaseRunner(object):
36 TestCaseRunner used to run
39 def run_testcase(self, testcase=None):
42 @param testcase: the testcase
43 @type testcase: unittest.TestCase
47 loader = unittest.TestLoader()
48 suite = loader.loadTestsFromTestCase(testcase)
50 # Create runner, and run testcase
51 io = StringIO.StringIO()
52 runner = unittest.TextTestRunner(stream=io)
53 results = runner.run(suite)
57 class TestAbstractBaseLeapTest(unittest.TestCase, _TestCaseRunner):
59 TestCase for BaseLeapTest abs
61 def test_abstract_base_class(self):
62 """test errors raised when setup/teardown not
64 class _BaseTest(BaseLeapTest):
65 def test_dummy_method(self):
68 def test_tautology(self):
71 results = self.run_testcase(_BaseTest)
73 # should be 2 errors: NotImplemented
74 # raised for setUp/tearDown
75 self.assertEquals(results.testsRun, 2)
76 self.assertEquals(len(results.failures), 0)
77 self.assertEquals(len(results.errors), 2)
80 class TestInitBaseLeapTest(BaseLeapTest):
82 testcase for testing initialization of BaseLeapTest
93 def test_path_is_changed(self):
94 """tests whether we have changed the PATH env var"""
95 os_path = os.environ['PATH']
96 self.assertTrue(os_path.startswith(self.tempdir))
98 def test_old_path_is_saved(self):
99 """tests whether we restore the PATH env var"""
100 self.assertTrue(len(self.old_path) > 1)
103 class TestCleanedBaseLeapTest(unittest.TestCase, _TestCaseRunner):
105 testcase for testing tempdir creation and cleanup
108 def test_tempdir_is_cleaned_after_tests(self):
110 test if a TestCase derived from BaseLeapTest
111 creates and cleans the temporal dir
113 class _BaseTest(BaseLeapTest):
115 """set global _tempdir to this instance tempdir"""
117 _tempdir = self.tempdir
123 def test_tempdir_created(self):
124 """test if tempdir was created"""
125 self.assertTrue(os.path.isdir(self.tempdir))
127 def test_tempdir_created_on_setupclass(self):
128 """test if tempdir is the one created by setupclass"""
129 self.assertEqual(_tempdir, self.tempdir)
131 results = self.run_testcase(_BaseTest)
132 self.assertEquals(results.testsRun, 2)
133 self.assertEquals(len(results.failures), 0)
134 self.assertEquals(len(results.errors), 0)
136 # did we cleaned the tempdir?
137 self.assertFalse(os.path.isdir(_tempdir))
139 if __name__ == "__main__":