def test_simple_message(self): class Module: class Test(unittest.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 test 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) with selftest(Module) as result: self.assertTrue(result.wasSuccessful())
def test_skip(self): class Module: class Test(ParameterizedTestCase): @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_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("ERROR", result.output)
def test_parameterized_tests_with_default_docstring(self): class Module: class Test(ParameterizedTestCase): @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_assertExpectations(self): class Module: class Test(exatest.TestCase): def test_x(self): self.expectTrue(False) self.assertExpectations() with selftest(Module) as result: self.assertIn("ExpectationError", result.output) self.assertNotIn("ERROR", 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_expectations_are_non_fatal(self): class Module: class Test(exatest.TestCase): def test_x(self): self.expectTrue(False) self.assertEquals(1, 2) with selftest(Module) as result: self.assertIn('False is not True', result.output) self.assertIn('1 != 2', result.output)
def test_parameterized_tests_with_decorator_expectedFailure(self): class Module: class Test(ParameterizedTestCase): @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(ParameterizedTestCase): @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_parameterized_tests_with_multiple_parameters(self): class Module: class Test(ParameterizedTestCase): @useData([(21, 42)]) def test_without_docstring(self, x, y): self.assertEquals(21, x) self.assertEquals(42, y) with selftest(Module) as result: self.assertTrue(result.wasSuccessful())
def test_large_parameterized_tests(self): class Module: class Test(ParameterizedTestCase): 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_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)
def test_assertExpectations_contextmanager(self): class Module: class Test(exatest.TestCase): def test_x(self): with self.expectations(): self.expectTrue(False) self.assertEquals(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_skipIf(self): class Module: class Test(ParameterizedTestCase): @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(unittest.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_expectedFailureIf_True_tests(self): class Module: class Test(unittest.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__.Test) ... expected failure', result.output) self.assertIn('\ntest_3 (__main__.Test) ... unexpected success', result.output) self.assertEqual(1, len(result.expectedFailures)) self.assertEqual(1, len(result.unexpectedSuccesses))
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_anonymous(self): class Module: class Test(unittest.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: url = urllib.urlopen('http://%s:%d/dummy' % httpd.address) data = url.readlines() self.assertIn('babelfish', '\n'.join(data)) with selftest(Module) as result: self.assertTrue(result.wasSuccessful())
def test_parameterized_tests_with_decorator_skipIf(self): class Module: class Test(ParameterizedTestCase): @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_non_esmtp(self): class Module: class Test(unittest.TestCase): def test_1(self): with SMTPServer(esmtp=False, debug=True) as smtpd: host, port = smtpd.address s = socket.create_connection(smtpd.address) buf1 = s.recv(4096) s.send('EHLO some.host.com\r\n') buf2 = s.recv(4096) s.send('QUIT\r\n') buf3 = s.recv(4096) s.close() self.assertIn('220', buf1) self.assertIn('502 Error', buf2) with selftest(Module) as result: self.assertTrue(result.wasSuccessful())
def test_anonymous(self): class Module: class Test(unittest.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_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_authenticated_user(self): class Module: class Test(unittest.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())