1 # -*- coding: utf-8 -*-
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 BaseLeapTest ...becase it's oh so meta
21 import unittest2 as unittest
28 from leap.testing.basetest import BaseLeapTest
30 _tempdir = None # global for tempdir checking
33 class _TestCaseRunner(object):
35 TestCaseRunner used to run BaseLeapTest
37 def run_testcase(self, testcase=None):
41 @param testcase: the testcase
42 @type testcase: unittest.TestCase
46 loader = unittest.TestLoader()
47 suite = loader.loadTestsFromTestCase(testcase)
49 # Create runner, and run testcase
50 io = StringIO.StringIO()
51 runner = unittest.TextTestRunner(stream=io)
52 results = runner.run(suite)
56 class TestAbstractBaseLeapTest(unittest.TestCase, _TestCaseRunner):
58 TestCase for BaseLeapTest abs
60 def test_abstract_base_class(self):
62 Test errors raised when setup/teardown not overloaded
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 creates and cleans the
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__":