Skip to content

Commit f9040f7

Browse files
committed
Make fixtures, testresources required for test suite
The tests are no longer packaged, so circular imports are less of a concern than previously. Note that this does not affect testtools itself, where both packages remain optional. Signed-off-by: Stephen Finucane <stephen@that.guru>
1 parent de2a7ce commit f9040f7

5 files changed

Lines changed: 76 additions & 122 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ requires-python = ">=3.10"
3434
Homepage = "https://github.com/testing-cabal/testtools"
3535

3636
[project.optional-dependencies]
37-
test = ["testscenarios", "testresources"]
37+
test = ["fixtures", "testscenarios", "testresources"]
3838
twisted = ["Twisted", "fixtures"]
3939
dev = [
4040
"ruff==0.15.7",

tests/test_fixturesupport.py

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import unittest
44

5+
import fixtures
6+
57
from testtools import (
68
TestCase,
79
content,
@@ -15,36 +17,25 @@
1517
ExtendedTestResult,
1618
)
1719

18-
try:
19-
import fixtures
20-
except ImportError:
21-
fixtures = None # type: ignore
22-
23-
if fixtures:
2420

25-
class LoggingFixture(fixtures.Fixture):
26-
def __init__(self, suffix: str = "", calls: list[str] | None = None) -> None:
27-
super().__init__()
28-
if calls is None:
29-
calls = []
30-
self.calls = calls
31-
self.suffix = suffix
21+
class LoggingFixture(fixtures.Fixture):
22+
def __init__(self, suffix: str = "", calls: list[str] | None = None) -> None:
23+
super().__init__()
24+
if calls is None:
25+
calls = []
26+
self.calls = calls
27+
self.suffix = suffix
3228

33-
def setUp(self) -> None:
34-
super().setUp()
35-
self.calls.append("setUp" + self.suffix)
36-
self.addCleanup(self.calls.append, "cleanUp" + self.suffix)
29+
def setUp(self) -> None:
30+
super().setUp()
31+
self.calls.append("setUp" + self.suffix)
32+
self.addCleanup(self.calls.append, "cleanUp" + self.suffix)
3733

38-
def reset(self) -> None:
39-
self.calls.append("reset" + self.suffix)
34+
def reset(self) -> None:
35+
self.calls.append("reset" + self.suffix)
4036

4137

4238
class TestFixtureSupport(TestCase):
43-
def setUp(self):
44-
super().setUp()
45-
if fixtures is None or LoggingFixture is None:
46-
self.skipTest("Need fixtures")
47-
4839
def test_useFixture(self):
4940
fixture = LoggingFixture()
5041

tests/test_run.py

Lines changed: 56 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from textwrap import dedent
1010
from unittest import TestSuite
1111

12+
import fixtures
13+
1214
import testtools
1315
from testtools import TestCase, run, skipUnless
1416
from testtools.matchers import (
@@ -17,29 +19,17 @@
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
4535
class 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"""
8169
from fixtures import Fixture
8270
from 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"""
136122
from testtools import TestCase, clone_test_with_new_id
137123
138124
class 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.

tests/test_testresult.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
from typing import Any
1919
from unittest import TestSuite
2020

21+
import testresources
22+
2123
from testtools import (
2224
CopyStreamResult,
2325
ExtendedToOriginalDecorator,
@@ -84,11 +86,6 @@
8486
run_with_stack_hidden,
8587
)
8688

87-
try:
88-
import testresources
89-
except ImportError:
90-
testresources = None
91-
9289

9390
def _utcfromtimestamp(t):
9491
return datetime.datetime.fromtimestamp(t, tz=datetime.timezone.utc).replace(
@@ -1477,11 +1474,6 @@ def _subDescription(self):
14771474

14781475

14791476
class TestResourcedToStreamDecorator(TestCase):
1480-
def setUp(self):
1481-
super().setUp()
1482-
if testresources is None:
1483-
self.skipTest("Need testresources")
1484-
14851477
def test_startMakeResource(self):
14861478
log = LoggingStreamResult()
14871479
result = ResourcedToStreamDecorator(log)

tests/test_testsuite.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import unittest
77
from pprint import pformat
88

9+
from fixtures import FunctionFixture
10+
911
from testtools import (
1012
ConcurrentStreamTestSuite,
1113
ConcurrentTestSuite,
@@ -20,11 +22,6 @@
2022

2123
from .helpers import LoggingResult
2224

23-
try:
24-
from fixtures import FunctionFixture
25-
except ImportError:
26-
FunctionFixture = None # type: ignore
27-
2825

2926
class Sample(TestCase):
3027
def __hash__(self):
@@ -348,11 +345,6 @@ def test_simple(self):
348345

349346

350347
class TestFixtureSuite(TestCase):
351-
def setUp(self):
352-
super().setUp()
353-
if FunctionFixture is None:
354-
self.skipTest("Need fixtures")
355-
356348
def test_fixture_suite(self):
357349
log: list[int | str] = []
358350

0 commit comments

Comments
 (0)