def test_subtest(self): for i in range(2): with self.subTest("check1", i=i): self.reboot() self.assertEqual(i, 0) self.assertTrue(True) for i in range(2): with self.subTest("check2", i=i): try: self.assertEqual(i, 0) finally: self.reboot() @unittest.skipUnless(hasattr(unittest.TestCase, "subTest"), "for Python3") def test_exception(self): for i in range(2): with self.subTest("check3", i=i): raise RuntimeError() with self.subTest("check4"): try: self.assertTrue(False) finally: raise RuntimeError() if __name__ == "__main__": dummy = pausable_unittest.dummypauser.Pauser() pausable_unittest.main(dummy, assertion_log=True)
# -*- coding: utf-8 -*- import sys import os.path ROOT_DIR = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])), os.pardir)) sys.path.append(ROOT_DIR) import pausable_unittest import testpauser import time import re class ExecCallbackTest(pausable_unittest.TestCase): def test_exec_callback(self): self.assertEqual(self.bat_path("base"), "base_sample", "bat_path should return XXX_sample") try: self.create_bat() except: self.assertFalse("create_bat should not raise any errors.") if __name__ == "__main__": pauser = testpauser.Pauser() pausable_unittest.main(pauser)
# -*- coding: utf-8 -*- import sys import os.path ROOT_DIR = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])), os.pardir)) sys.path.append(ROOT_DIR) import pausable_unittest import testpauser import time class FailureTest(pausable_unittest.TestCase): def test_failure_exec_for_reboot(self): self.exec_for_reboot("invalid_command") if __name__ == "__main__": pausable_unittest.main(testpauser.Pauser())
# -*- coding: utf-8 -*- import sys import os.path ROOT_DIR = os.path.normpath( os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])), os.pardir)) sys.path.append(ROOT_DIR) import pausable_unittest import testpauser import pausable_unittest.dummypauser import time import re class PauserTest(pausable_unittest.TestCase): def test_pauser(self): self.assertFalse( hasattr(self, "exec_for_reboot"), "method for testpauser should not copied to TestCase") if __name__ == "__main__": dummy = pausable_unittest.dummypauser.Pauser() test = testpauser.Pauser() pausable_unittest.main(dummy)
def test_options(self): self.assertEqual(self.assertion_log, self.options.get("test", False)) def test_exec_for_reboot(self): for i in range(3): start = time.time() if sys.platform == "win32": self.exec_for_reboot("cmd /c echo test_exec_for_reboot %d" % i) else: self.exec_for_reboot("bash -c 'echo test_exec_for_reboot %d'" % i) end = time.time() margin = 3 self.assertTrue(start + 0.75 < end, "start + 0.75 should be less than end." ) self.assertTrue(start + 1 + margin > end, "start + 1 + margin should be more than end.") def test_assert_raises(self): num = 1 with self.assertRaises(ZeroDivisionError, "msg") as cm: self.reboot() 1 / (num - 1) self.assertEqual(type(cm.exception), ZeroDivisionError) self.assertRaises(ZeroDivisionError, lambda x: 1 / x, 0) if __name__ == "__main__": if len(sys.argv) >= 2 and sys.argv[1] == "debug": pausable_unittest.main(testpauser.Pauser(), loglevel=logging.DEBUG, assertion_log=True, options={"test": True}) else: pausable_unittest.main(testpauser.Pauser())
import pausable_unittest import pausable_unittest.dummypauser import time class SampleTest(pausable_unittest.TestCase): def test_sample1(self): for x in range(2): start = time.time() self.reboot() end = time.time() self.assertTrue(start + 1 < end, "start should be less than end") def test_sample2(self): start = time.time() self.shutdown() end = time.time() self.assertTrue(start + 1 < end, "start should be less than end") if __name__ == "__main__": pausable_unittest.main(pausable_unittest.dummypauser.Pauser())
import logging class NullHandler(logging.Handler): """A Handler that does nothing.""" def emit(self, record): pass # logger shares Logger.manager, so it prevents pickle. logger = logging.getLogger("another_log") logger.addHandler(NullHandler()) class SampleTest(pausable_unittest.TestCase): def test_dummy(self): self.reboot() self.assertTrue(True) def command_after_test(info): results = info["result"].results if len(results) == 1 and results[0][0] == "success": with open("command_after_test.txt", "w") as f: f.write("done") if __name__ == "__main__": pausable_unittest.main(testpauser.Pauser(), command_after_test=command_after_test)
# -*- coding: utf-8 -*- import sys import os.path ROOT_DIR = os.path.normpath( os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])), os.pardir)) sys.path.append(ROOT_DIR) import pausable_unittest import testpauser import time import re class ExecCallbackTest(pausable_unittest.TestCase): def test_exec_callback(self): self.assertEqual(self.bat_path("base"), "base_sample", "bat_path should return XXX_sample") try: self.create_bat() except: self.assertFalse("create_bat should not raise any errors.") if __name__ == "__main__": pauser = testpauser.Pauser() pausable_unittest.main(pauser)
import pausable_unittest import pausable_unittest.dummypauser import time class SampleTest(pausable_unittest.TestCase): def test_sample1(self): for x in range(2): start = time.time() self.reboot() end = time.time() self.assertTrue(start + 1 < end, "start should be less than end") def test_sample2(self): start = time.time() self.shutdown() end = time.time() self.assertTrue(start + 1 < end, "start should be less than end") if __name__ == "__main__": pausable_unittest.main(pausable_unittest.dummypauser.Pauser())