def test_unorderable_types(testdir):
testdir.makepyfile("""
import unittest
class TestJoinEmpty(unittest.TestCase):
pass
def make_test():
class Test(unittest.TestCase):
pass
Test.__name__ = "TestFoo"
return Test
TestFoo = make_test()
""")
result = testdir.runpytest()
assert "TypeError" not in result.stdout.str()
assert result.ret == EXIT_NOTESTSCOLLECTED
python类TestCase()的实例源码
def test_unittest_unexpected_failure(testdir):
testdir.makepyfile("""
import unittest
class MyTestCase(unittest.TestCase):
@unittest.expectedFailure
def test_func1(self):
assert 0
@unittest.expectedFailure
def test_func2(self):
assert 1
""")
result = testdir.runpytest("-rxX")
result.stdout.fnmatch_lines([
"*XFAIL*MyTestCase*test_func1*",
"*XPASS*MyTestCase*test_func2*",
"*1 xfailed*1 xpass*",
])
def test_no_teardown_if_setupclass_failed(testdir):
testpath = testdir.makepyfile("""
import unittest
class MyTestCase(unittest.TestCase):
x = 0
@classmethod
def setUpClass(cls):
cls.x = 1
assert False
def test_func1(self):
cls.x = 10
@classmethod
def tearDownClass(cls):
cls.x = 100
def test_notTornDown():
assert MyTestCase.x == 1
""")
reprec = testdir.inline_run(testpath)
reprec.assertoutcome(passed=1, failed=1)
def testIsinstance(self):
self.assert_(isinstance(u'hi', types.StringTypes))
self.assert_(isinstance(self, unittest.TestCase))
# I'm pretty sure it's impossible to implement this
# without replacing isinstance on 2.2 as well :(
# self.assert_(isinstance({}, dict))
def test_visitPyunitCase(self):
"""
Test that a stdlib test case in a suite gets visited.
"""
class PyunitCase(pyunit.TestCase):
def test_foo(self):
pass
test = PyunitCase('test_foo')
TestSuite([test]).visit(self.visitor)
self.assertEqual(self.visitor.calls, [test])
def test_defaultIsSuccessful(self):
"""
Test that L{unittest.TestCase} itself can be instantiated, run, and
reported as being successful.
"""
test = unittest.TestCase()
test.run(self.result)
self.assertSuccessful(test, self.result)
def makeTestFixtures(self):
class MockTest(unittest.TestCase):
def test_foo(test):
self.log.append('test_foo')
self.test = MockTest('test_foo')
self.suite = runner.TestSuite()
def test_failIf_matches_assertNot(self):
asserts = reflect.prefixedMethods(unittest.TestCase, 'assertNot')
failIfs = reflect.prefixedMethods(unittest.TestCase, 'failIf')
self.failUnlessEqual(dsu(asserts, self._name),
dsu(failIfs, self._name))
def test_success(self):
class SuccessTest(TestCase):
ran = False
def test_foo(s):
s.ran = True
test = SuccessTest('test_foo')
result = pyunit.TestResult()
test.run(result)
self.failUnless(test.ran)
self.assertEqual(1, result.testsRun)
self.failUnless(result.wasSuccessful())
def test_failure(self):
class FailureTest(TestCase):
ran = False
def test_foo(s):
s.ran = True
s.fail('boom!')
test = FailureTest('test_foo')
result = pyunit.TestResult()
test.run(result)
self.failUnless(test.ran)
self.assertEqual(1, result.testsRun)
self.assertEqual(1, len(result.failures))
self.failIf(result.wasSuccessful())
def test_error(self):
class ErrorTest(TestCase):
ran = False
def test_foo(s):
s.ran = True
1/0
test = ErrorTest('test_foo')
result = pyunit.TestResult()
test.run(result)
self.failUnless(test.ran)
self.assertEqual(1, result.testsRun)
self.assertEqual(1, len(result.errors))
self.failIf(result.wasSuccessful())
def visit(self, visitor):
"""
Call the given visitor with the original, standard library, test case
that C{self} wraps. See L{unittest.TestCase.visit}.
"""
visitor(self._test)
def _bail(self):
from twisted.internet import reactor
d = defer.Deferred()
reactor.addSystemEventTrigger('after', 'shutdown',
lambda: d.callback(None))
reactor.fireSystemEvent('shutdown') # radix's suggestion
treactor = interfaces.IReactorThreads(reactor, None)
if treactor is not None:
treactor.suggestThreadPoolSize(0)
# As long as TestCase does crap stuff with the reactor we need to
# manually shutdown the reactor here, and that requires util.wait
# :(
# so that the shutdown event completes
unittest.TestCase('mktemp')._wait(d)
def visit(self, visitor):
"""
See L{unittest.TestCase.visit}.
"""
visitor(self)
def loadClass(self, klass):
"""
Given a class which contains test cases, return a sorted list of
C{TestCase} instances.
"""
if not (isinstance(klass, type) or isinstance(klass, types.ClassType)):
raise TypeError("%r is not a class" % (klass,))
if not isTestCase(klass):
raise ValueError("%r is not a test case" % (klass,))
names = self.getTestCaseNames(klass)
tests = self.sort([self._makeCase(klass, self.methodPrefix+name)
for name in names])
return self.suiteFactory(tests)
def getTestCaseNames(self, klass):
"""
Given a class that contains C{TestCase}s, return a list of names of
methods that probably contain tests.
"""
return reflect.prefixedMethodNames(klass, self.methodPrefix)
def loadMethod(self, method):
"""
Given a method of a C{TestCase} that represents a test, return a
C{TestCase} instance for that test.
"""
if not isinstance(method, types.MethodType):
raise TypeError("%r not a method" % (method,))
return self._makeCase(method.im_class, method.__name__)