def test_simple_message(self): class Module: class Test(exatest.TestCase): def test_1(self): with SMTPServer(debug=True) as smtpd: host, port = smtpd.address self.assertNotEqual(0, port) client = smtplib.SMTP(host, port, timeout=5) client.set_debuglevel(1) msg = MIMEText('Test mail') msg['Subject'] = 'Little exatest mail' msg['From'] = '*****@*****.**' msg['To'] = '*****@*****.**' client.sendmail( '*****@*****.**', ('*****@*****.**', '*****@*****.**'), msg.as_string()) client.quit() self.assertEqual(1, len(smtpd.messages)) self.assertEqual('*****@*****.**', smtpd.messages[0].sender) self.assertIn('*****@*****.**', smtpd.messages[0].recipients) self.assertIn('Test mail', smtpd.messages[0].body.decode("utf-8")) with selftest(Module) as result: self.assertTrue(result.wasSuccessful())
def test_missing_assertExpectations_results_in_error(self): class Module: class Test(exatest.TestCase): def test_x(self): self.expectTrue(False) with selftest(Module) as result: self.assertIn("ExpectationError", result.output) self.assertIn("FAILED", result.output)
def test_getattr_unknown_assert(self): class Module: class Test(exatest.TestCase): def test_x(self): self.expectFoo() with selftest(Module) as result: self.assertIn( "AttributeError: 'Test' object has no attribute 'expectFoo'", result.output)
def test_parameterized_tests_with_default_docstring(self): class Module: class Test(exatest.TestCase): @useData([(21, ), (42, )]) def test_without_docstring(self, x): pass with selftest(Module) as result: self.assertIn('\ndata: (21,) ... ok', result.output) self.assertIn('\ndata: (42,) ... ok', result.output)
def test_parameterized_tests_with_multiple_parameters(self): class Module: class Test(exatest.TestCase): @useData([(21, 42)]) def test_without_docstring(self, x, y): self.assertEqual(21, x) self.assertEqual(42, y) with selftest(Module) as result: self.assertTrue(result.wasSuccessful())
def test_expectations_are_non_fatal(self): class Module: class Test(exatest.TestCase): def test_x(self): self.expectTrue(False) self.assertEqual(1, 2) with selftest(Module) as result: self.assertIn('False is not true', result.output) self.assertIn('1 != 2', result.output)
def test_skip(self): class Module: class Test(exatest.TestCase): @skip('Some Reason') def test_1(self): pass with selftest(Module) as result: self.assertEqual(1, len(result.skipped)) self.assertIn('Some Reason', result.output)
def test_parameterized_tests_with_decorator_expectedFailure(self): class Module: class Test(exatest.TestCase): @useData((x, ) for x in range(2)) @expectedFailure def test_skipped_with_param(self, x): x / 0 with selftest(Module) as result: self.assertEqual(2, len(result.expectedFailures))
def test_parameterized_tests_with_decorator_skip(self): class Module: class Test(exatest.TestCase): @useData((x, ) for x in range(2)) @skip('some reason') def test_skipped_with_param(self, x): pass with selftest(Module) as result: self.assertEqual(2, len(result.skipped))
def test_large_parameterized_tests(self): class Module: class Test(exatest.TestCase): data = [(x, ) for x in range(1000)] @useData(data) def test_foo(self, x): self.assertTrue(x % 3 != 0) with selftest(Module) as result: self.assertEqual(1000, result.testsRun) self.assertEqual(334, len(result.failures))
def test_assertExpectations_contextmanager(self): class Module: class Test(exatest.TestCase): def test_x(self): with self.expectations(): self.expectTrue(False) self.assertEqual(1, 2) with selftest(Module) as result: self.assertIn("ExpectationError", result.output) self.assertNotIn('1 != 2', result.output) self.assertNotIn("ERROR", result.output)
def test_getattr(self): class Module: class Test(exatest.TestCase): def assertFoo(self): self.Fail("FOO") def test_x(self): self.expectFoo() with selftest(Module) as result: self.assertIn('FOO', result.output) self.assertIn('FAILED', result.output)
def test_metatest(self): class Module: class Test(exatest.TestCase): def test_pass(self): pass def test_fail(self): self.fail() with selftest(Module) as result: self.assertEqual(1, len(result.failures)) self.assertEqual(2, result.testsRun) self.assertIn('test_pass', result.output) self.assertIn('test_fail', result.output)
def test_skipIf(self): class Module: class Test(exatest.TestCase): @skipIf(False, 'Some Reason 1') def test_1(self): pass @skipIf(True, 'Some Reason 2') def test_2(self): pass with selftest(Module) as result: self.assertEqual(1, len(result.skipped)) self.assertNotIn('Some Reason 1', result.output) self.assertIn('Some Reason 2', result.output)
def test_metatest(self): class Module: class Test(exatest.TestCase): def test_pass(self): self.assertExpectations() def test_fail(self): self._expectations.append('some traceback') self.assertExpectations() with selftest(Module) as result: self.assertEqual(1, len(result.failures)) self.assertEqual(2, result.testsRun) self.assertIn('test_pass', result.output) self.assertIn('test_fail', result.output)
def test_parameterized_tests_with_decorator_skipIf(self): class Module: class Test(exatest.TestCase): @useData((x, ) for x in range(2)) @skipIf(False, 'Some Reason 1') def test_1(self, x): self.assertIn(x, range(2)) @useData((x, ) for x in range(2)) @skipIf(True, 'Some Reason 2') def test_2(self, x): pass with selftest(Module) as result: self.assertEqual(2, len(result.skipped)) self.assertNotIn('Some Reason 1', result.output) self.assertIn('Some Reason 2', result.output)
def test_server_is_chdir_safe(self): class Module: class Test(exatest.TestCase): def test_server_is_chdir_safe(self): cwd = os.getcwd() with tempdir() as tmp: self.assertEqual(cwd, os.getcwd()) with FTPServer(tmp) as ftpd: # Without sleep the FTPServer starts and stops immediately # which might cause a race condition during shutdown time.sleep(0.2) self.assertEqual(cwd, os.getcwd()) self.assertEqual(cwd, os.getcwd()) self.assertEqual(cwd, os.getcwd()) with selftest(Module) as result: self.assertTrue(result.wasSuccessful())
def test_traceback_is_in_right_order(self): class Module: class Test(exatest.TestCase): def first(self): self.expectTrue(False) def second(self): self.first() def third(self): self.second() def test_x(self): self.third() with selftest(Module) as result: self.assertTrue( re.search('third.*second.*first', result.output, re.DOTALL))
def test_anonymous(self): class Module: class Test(exatest.TestCase): def test_1(self): with tempdir() as tmp: with open(os.path.join(tmp, 'dummy'), 'w'): pass with FTPServer(tmp) as ftpd: ftp = ftplib.FTP() ftp.connect(*ftpd.address) ftp.login() data = [] ls = ftp.retrlines('LIST', data.append) ftp.quit() self.assertIn('dummy', '\n'.join(data)) with selftest(Module) as result: self.assertTrue(result.wasSuccessful())
def test_anonymous(self): class Module: class Test(exatest.TestCase): def test_1(self): with tempdir() as tmp: with open(os.path.join(tmp, 'dummy'), 'w') as f: f.write('babelfish') with HTTPServer(tmp) as httpd: with urllib.request.urlopen('http://%s:%d/dummy' % httpd.address) as url: data = [ line.decode("utf-8") for line in url.readlines() ] self.assertIn('babelfish', '\n'.join(data)) with selftest(Module) as result: self.assertTrue(result.wasSuccessful())
def test_esmtp(self): """ Python3 supports ESMTP natively (while Python2 did not) """ class Module: class Test(exatest.TestCase): def test_1(self): with SMTPServer(debug=True) as smtpd: host, port = smtpd.address s = socket.create_connection(smtpd.address) buf1 = s.recv(4096) s.send( b'EHLO some.host.com\r\n') #Send an ESMTP command buf2 = s.recv(4096) s.send(b'QUIT\r\n') buf3 = s.recv(4096) s.close() self.assertIn('220', buf1.decode("utf-8")) self.assertIn('250', buf2.decode("utf-8")) with selftest(Module) as result: self.assertTrue(result.wasSuccessful())
def test_authenticated_user(self): class Module: class Test(exatest.TestCase): def test_1(self): with tempdir() as tmp: with open(os.path.join(tmp, 'dummy'), 'w'): pass auth = DummyAuthorizer() auth.add_user('user', 'passwd', tmp, perm='elradfmw') with FTPServer(tmp, authorizer=auth) as ftpd: ftp = ftplib.FTP() ftp.connect(*ftpd.address) ftp.login('user', 'passwd') ftp.mkd('some_dir') data = [] ls = ftp.retrlines('LIST', data.append) ftp.quit() self.assertIn('dummy', '\n'.join(data)) self.assertIn('some_dir', '\n'.join(data)) with selftest(Module) as result: self.assertTrue(result.wasSuccessful())
def test_expectedFailureIf_True_tests(self): class Module: class Test(exatest.TestCase): def test_1(self): pass @expectedFailureIf(True) def test_2(self): self.fail() @expectedFailureIf(True) def test_3(self): pass with selftest(Module) as result: self.assertIn( '\ntest_2 (__main__.ConditionalTestCaseTest.test_expectedFailureIf_True_tests.<locals>.Module.Test) ... expected failure', result.output) self.assertIn( '\ntest_3 (__main__.ConditionalTestCaseTest.test_expectedFailureIf_True_tests.<locals>.Module.Test) ... unexpected success', result.output) self.assertEqual(1, len(result.expectedFailures)) self.assertEqual(1, len(result.unexpectedSuccesses))