99from textwrap import dedent
1010from unittest import TestSuite
1111
12+ import fixtures
13+
1214import testtools
1315from testtools import TestCase , run , skipUnless
1416from testtools .matchers import (
1719 MatchesRegex ,
1820)
1921
20- try :
21- import fixtures
22- except ImportError :
23- fixtures = None # type: ignore
24-
25- try :
26- import testresources
27- except ImportError :
28- testresources = None
29-
30-
31- if fixtures :
3222
33- class SampleTestFixture (fixtures .Fixture ):
34- """Creates testtools.runexample temporarily."""
23+ class SampleTestFixture (fixtures .Fixture ):
24+ """Creates testtools.runexample temporarily."""
3525
36- def __init__ (self , broken = False ):
37- """Create a SampleTestFixture.
26+ def __init__ (self , broken = False ):
27+ """Create a SampleTestFixture.
3828
39- :param broken: If True, the sample file will not be importable.
40- """
41- if not broken :
42- init_contents = b"""\
29+ :param broken: If True, the sample file will not be importable.
30+ """
31+ if not broken :
32+ init_contents = b"""\
4333 from testtools import TestCase
4434
4535class TestFoo(TestCase):
@@ -51,33 +41,31 @@ def test_suite():
5141 from unittest import TestLoader
5242 return TestLoader().loadTestsFromName(__name__)
5343"""
54- else :
55- init_contents = b"class not in\n "
56- self .package = fixtures .PythonPackage (
57- "runexample" , [("__init__.py" , init_contents )]
58- )
59-
60- def setUp (self ):
61- super ().setUp ()
62- self .useFixture (self .package )
63- testtools .__path__ .append (self .package .base )
64- self .addCleanup (testtools .__path__ .remove , self .package .base )
65- self .addCleanup (sys .modules .pop , "testtools.runexample" , None )
44+ else :
45+ init_contents = b"class not in\n "
46+ self .package = fixtures .PythonPackage (
47+ "runexample" , [("__init__.py" , init_contents )]
48+ )
6649
50+ def setUp (self ):
51+ super ().setUp ()
52+ self .useFixture (self .package )
53+ testtools .__path__ .append (self .package .base )
54+ self .addCleanup (testtools .__path__ .remove , self .package .base )
55+ self .addCleanup (sys .modules .pop , "testtools.runexample" , None )
6756
68- if fixtures and testresources :
6957
70- class SampleResourcedFixture (fixtures .Fixture ):
71- """Creates a test suite that uses testresources."""
58+ class SampleResourcedFixture (fixtures .Fixture ):
59+ """Creates a test suite that uses testresources."""
7260
73- def __init__ (self ):
74- super ().__init__ ()
75- self .package = fixtures .PythonPackage (
76- "resourceexample" ,
77- [
78- (
79- "__init__.py" ,
80- b"""
61+ def __init__ (self ):
62+ super ().__init__ ()
63+ self .package = fixtures .PythonPackage (
64+ "resourceexample" ,
65+ [
66+ (
67+ "__init__.py" ,
68+ b"""
8169from fixtures import Fixture
8270from testresources import (
8371 FixtureResource,
@@ -109,30 +97,28 @@ def test_suite():
10997 from unittest import TestLoader
11098 return OptimisingTestSuite(TestLoader().loadTestsFromName(__name__))
11199""" ,
112- )
113- ],
114- )
115-
116- def setUp (self ):
117- super ().setUp ()
118- self .useFixture (self .package )
119- self .addCleanup (testtools .__path__ .remove , self .package .base )
120- testtools .__path__ .append (self .package .base )
100+ )
101+ ],
102+ )
121103
104+ def setUp (self ):
105+ super ().setUp ()
106+ self .useFixture (self .package )
107+ self .addCleanup (testtools .__path__ .remove , self .package .base )
108+ testtools .__path__ .append (self .package .base )
122109
123- if fixtures :
124110
125- class SampleLoadTestsPackage (fixtures .Fixture ):
126- """Creates a test suite package using load_tests."""
111+ class SampleLoadTestsPackage (fixtures .Fixture ):
112+ """Creates a test suite package using load_tests."""
127113
128- def __init__ (self ):
129- super ().__init__ ()
130- self .package = fixtures .PythonPackage (
131- "discoverexample" ,
132- [
133- (
134- "__init__.py" ,
135- b"""
114+ def __init__ (self ):
115+ super ().__init__ ()
116+ self .package = fixtures .PythonPackage (
117+ "discoverexample" ,
118+ [
119+ (
120+ "__init__.py" ,
121+ b"""
136122from testtools import TestCase, clone_test_with_new_id
137123
138124class TestExample(TestCase):
@@ -143,22 +129,17 @@ def load_tests(loader, tests, pattern):
143129 tests.addTest(clone_test_with_new_id(tests._tests[1]._tests[0], "fred"))
144130 return tests
145131""" ,
146- )
147- ],
148- )
149-
150- def setUp (self ):
151- super ().setUp ()
152- self .useFixture (self .package )
153- self .addCleanup (sys .path .remove , self .package .base )
154-
132+ )
133+ ],
134+ )
155135
156- class TestRun (TestCase ):
157136 def setUp (self ):
158137 super ().setUp ()
159- if fixtures is None :
160- self .skipTest ( "Need fixtures" )
138+ self . useFixture ( self . package )
139+ self .addCleanup ( sys . path . remove , self . package . base )
161140
141+
142+ class TestRun (TestCase ):
162143 def test_run_custom_list (self ):
163144 self .useFixture (SampleTestFixture ())
164145 tests = []
@@ -347,8 +328,6 @@ def test_run_load_list(self):
347328 )
348329
349330 def test_load_list_preserves_custom_suites (self ):
350- if testresources is None :
351- self .skipTest ("Need testresources" )
352331 self .useFixture (SampleResourcedFixture ())
353332 # We load two tests, not loading one. Both share a resource, so we
354333 # should see just one resource setup occur.
0 commit comments