json_out = self.json_append( t, ERROR, json_out ) for t, _ in self.skipped: json_out = self.json_append( t, SKIP, json_out ) return json_out def tearDown(self): self.assertEqual([], self.verificationErrors) if __name__ == '__main__': # redirector default output of unittest to /dev/null with open( os.devnull, 'w' ) as null_stream: runner = TextTestRunner( stream=null_stream ) runner.resultclass = AppDynamicsJob TESTLINK_API_PYTHON_SERVER_URL = 'http://127.0.0.1/testlink/lib/api/xmlrpc/v1/xmlrpc.php' #wpisz swój klucz w dół TESTLINK_API_PYTHON_DEVKEY = '724497bfde0cc05904c85a68a1263c6d' tl_helper = TestLinkHelper(TESTLINK_API_PYTHON_SERVER_URL, TESTLINK_API_PYTHON_DEVKEY) myTestLink = tl_helper.connect(TestlinkAPIClient) print(myTestLink.connectionInfo()) print(myTestLink.countProjects()) tc_info = myTestLink.getTestCase(None, testcaseexternalid='26-2') print(tc_info) tc_info = myTestLink.getProjectTestPlans('1') print(tc_info) # tls.reportTCResult(4, 2, 'SampleBuild', 'f', 'some notes', user='******', platformid='1') myTestLink.reportTCResult(None, 2, None, 'p', 'some notes', guess=True,
for t, _ in self.errors: json_out = self.json_append(t, ERROR, json_out) for t, _ in self.skipped: json_out = self.json_append(t, SKIP, json_out) return json_out if __name__ == '__main__': # redirector default output of unittest to /dev/null unittest.main(testRunner=xmlrunner.XMLTestRunner( output="./python_unittests_xml")) with open(os.devnull, 'w') as null_stream: # new a runner and overwrite resultclass of runner runner = TextTestRunner(stream=null_stream) runner.resultclass = JsonTestResult # create a testsuite suite = TestLoader().loadTestsFromTestCase(TestSimple) # run the testsuite result = runner.run(suite) # print json output # pprint(result.jsonify()) TESTLINK_API_PYTHON_SERVER_URL = "http://192.168.43.122/lib/api/xmlrpc/v1/xmlrpc.php" TESTLINK_API_PYTHON_DEVKEY = "425a0eba97d42c8005a07c7613e359c1" # tls = testlink.TestLinkHelper( TESTLINK_API_PYTHON_SERVER_URL, TESTLINK_API_PYTHON_DEVKEY).connect(testlink.TestlinkAPIClient)
def fib(n): """ This function calculate fib number. Example: >>> fib(10) 55 >>> fib(-1) Traceback (most recent call last): ... ValueError """ if n < 0: raise ValueError('') return 1 if n<=2 else fib(n-1) + fib(n-2) if __name__ == "__main__": import doctest doctest.testmod() output: $ python demo_doctest.py -v Trying: fib(10) Expecting: 55 ok Trying: fib(10) Expecting: 55 ok Trying: fib(-1) Expecting: Traceback (most recent call last): ... ValueError ok 2 items passed all tests: 1 tests in __main__ 2 tests in __main__.fib 3 tests in 2 items. 3 passed and 0 failed. Test passed. Re-using doctest to unittest import unittest import doctest """ This is an example of doctest >>> fib(10) 55 """ def fib(n): """ This function calculate fib number. Example: >>> fib(10) 55 >>> fib(-1) Traceback (most recent call last): ... ValueError """ if n < 0: raise ValueError('') return 1 if n<=2 else fib(n-1) + fib(n-2) if __name__ == "__main__": finder = doctest.DocTestFinder() suite = doctest.DocTestSuite(test_finder=finder) unittest.TextTestRunner(verbosity=2).run(suite) output: fib (__main__) Doctest: __main__.fib ... ok ---------------------------------------------------------------------- Ran 1 test in 0.023s OK Customize test report from unittest import ( TestCase, TestLoader, TextTestResult, TextTestRunner) from pprint import pprint import unittest import os OK = 'ok' FAIL = 'fail' ERROR = 'error' SKIP = 'skip' class JsonTestResult(TextTestResult): def __init__(self, stream, descriptions, verbosity): super_class = super(JsonTestResult, self) super_class.__init__(stream, descriptions, verbosity) # TextTestResult has no successes attr self.successes = [] def addSuccess(self, test): # addSuccess do nothing, so we need to overwrite it. super(JsonTestResult, self).addSuccess(test) self.successes.append(test) def json_append(self, test, result, out): suite = test.__class__.__name__ if suite not in out: out[suite] = {OK: [], FAIL: [], ERROR:[], SKIP: []} if result is OK: out[suite][OK].append(test._testMethodName) elif result is FAIL: out[suite][FAIL].append(test._testMethodName) elif result is ERROR: out[suite][ERROR].append(test._testMethodName) elif result is SKIP: out[suite][SKIP].append(test._testMethodName) else: raise KeyError("No such result: {}".format(result)) return out def jsonify(self): json_out = dict() for t in self.successes: json_out = self.json_append(t, OK, json_out) for t, _ in self.failures: json_out = self.json_append(t, FAIL, json_out) for t, _ in self.errors: json_out = self.json_append(t, ERROR, json_out) for t, _ in self.skipped: json_out = self.json_append(t, SKIP, json_out) return json_out class TestSimple(TestCase): def test_ok_1(self): foo = True self.assertTrue(foo) def test_ok_2(self): bar = True self.assertTrue(bar) def test_fail(self): baz = False self.assertTrue(baz) def test_raise(self): raise RuntimeError @unittest.skip("Test skip") def test_skip(self): raise NotImplementedError if __name__ == '__main__': # redirector default output of unittest to /dev/null with open(os.devnull, 'w') as null_stream: # new a runner and overwrite resultclass of runner runner = TextTestRunner(stream=null_stream) runner.resultclass = JsonTestResult # create a testsuite suite = TestLoader().loadTestsFromTestCase(TestSimple) # run the testsuite result = runner.run(suite) # print json output pprint(result.jsonify()) output: $ python test.py {'TestSimple': {'error': ['test_raise'], 'fail': ['test_fail'], 'ok': ['test_ok_1', 'test_ok_2'], 'skip': ['test_skip']}} Mock - using @patch substitute original method # python-3.3 or above >>> from unittest.mock import patch >>> import os >>> def fake_remove(path, *a, **k): ... print("remove done") ... >>> @patch('os.remove', fake_remove) ... def test(): ... try: ... os.remove('%$!?&*') # fake os.remove ... except OSError as e: ... print(e) ... else: ... print('test success') ... >>> test() remove done test success Note Without mock, above test will always fail. >>> import os >>> def test(): ... try: ... os.remove('%$!?&*') ... except OSError as e: ... print(e) ... else: ... print('test success') ... >>> test() [Errno 2] No such file or directory: '%$!?&*' What with unittest.mock.patch do? from unittest.mock import patch import os PATH = '$@!%?&' def fake_remove(path): print("Fake remove") class SimplePatch: def __init__(self, target, new): self._target = target self._new = new def get_target(self, target): target, attr = target.rsplit('.', 1) getter = __import__(target) return getter, attr def __enter__(self): orig, attr = self.get_target(self._target) self.orig, self.attr = orig, attr self.orig_attr = getattr(orig, attr) setattr(orig, attr, self._new) return self._new def __exit__(self, *exc_info): setattr(self.orig, self.attr, self.orig_attr) del self.orig_attr print('---> inside unittest.mock.patch scope') with patch('os.remove', fake_remove): os.remove(PATH) print('---> inside simple patch scope') with SimplePatch('os.remove', fake_remove): os.remove(PATH) print('---> outside patch scope') try: os.remove(PATH) except OSError as e: print(e) output: $ python3 simple_patch.py ---> inside unittest.mock.patch scope Fake remove ---> inside simple patch scope Fake remove ---> outside patch scope [Errno 2] No such file or directory: '$@!%?&' Mock - substitute open >>> import urllib >>> from unittest.mock import patch, mock_open >>> def send_req(url): ... with urllib.request.urlopen(url) as f: ... if f.status == 200: ... return f.read() ... raise urllib.error.URLError ... >>> fake_html = b'<html><h1>Mock Content</h1></html>' >>> mock_urlopen = mock_open(read_data=fake_html) >>> ret = mock_urlopen.return_value >>> ret.status = 200 >>> @patch('urllib.request.urlopen', mock_urlopen) ... def test_send_req_success(): ... try: ... ret = send_req('http://www.mockurl.com') ... assert ret == fake_html ... except Exception as e: ... print(e) ... else: ... print('test send_req success') ... >>> test_send_req_success() test send_req success >>> ret = mock_urlopen.return_value >>> ret.status = 404 >>> @patch('urllib.request.urlopen', mock_urlopen) ... def test_send_req_fail(): ... try: ... ret = send_req('http://www.mockurl.com') ... assert ret == fake_html ... except Exception as e: ... print('test fail success') ... >>> test_send_req_fail() test fail success