Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ requires-python = ">=3.10"
Homepage = "https://github.com/testing-cabal/testtools"

[project.optional-dependencies]
test = ["testscenarios", "testresources"]
test = ["fixtures", "testscenarios", "testresources"]
twisted = ["Twisted", "fixtures"]
dev = [
"ruff==0.15.7",
Expand Down
30 changes: 17 additions & 13 deletions tests/test_fixturesupport.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import unittest

import fixtures

from testtools import (
TestCase,
content,
Expand All @@ -15,23 +17,25 @@
ExtendedTestResult,
)

try:
import fixtures
except ImportError:
fixtures = None # type: ignore

try:
from fixtures.tests.helpers import LoggingFixture
except ImportError:
LoggingFixture = None

class LoggingFixture(fixtures.Fixture):
def __init__(self, suffix: str = "", calls: list[str] | None = None) -> None:
super().__init__()
if calls is None:
calls = []
self.calls = calls
self.suffix = suffix

class TestFixtureSupport(TestCase):
def setUp(self):
def setUp(self) -> None:
super().setUp()
if fixtures is None or LoggingFixture is None:
self.skipTest("Need fixtures")
self.calls.append("setUp" + self.suffix)
self.addCleanup(self.calls.append, "cleanUp" + self.suffix)

def reset(self) -> None:
self.calls.append("reset" + self.suffix)


class TestFixtureSupport(TestCase):
def test_useFixture(self):
fixture = LoggingFixture()

Expand Down
133 changes: 56 additions & 77 deletions tests/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from textwrap import dedent
from unittest import TestSuite

import fixtures

import testtools
from testtools import TestCase, run, skipUnless
from testtools.matchers import (
Expand All @@ -17,29 +19,17 @@
MatchesRegex,
)

try:
import fixtures
except ImportError:
fixtures = None # type: ignore

try:
import testresources
except ImportError:
testresources = None


if fixtures:

class SampleTestFixture(fixtures.Fixture):
"""Creates testtools.runexample temporarily."""
class SampleTestFixture(fixtures.Fixture):
"""Creates testtools.runexample temporarily."""

def __init__(self, broken=False):
"""Create a SampleTestFixture.
def __init__(self, broken=False):
"""Create a SampleTestFixture.

:param broken: If True, the sample file will not be importable.
"""
if not broken:
init_contents = b"""\
:param broken: If True, the sample file will not be importable.
"""
if not broken:
init_contents = b"""\
from testtools import TestCase

class TestFoo(TestCase):
Expand All @@ -51,33 +41,31 @@ def test_suite():
from unittest import TestLoader
return TestLoader().loadTestsFromName(__name__)
"""
else:
init_contents = b"class not in\n"
self.package = fixtures.PythonPackage(
"runexample", [("__init__.py", init_contents)]
)

def setUp(self):
super().setUp()
self.useFixture(self.package)
testtools.__path__.append(self.package.base)
self.addCleanup(testtools.__path__.remove, self.package.base)
self.addCleanup(sys.modules.pop, "testtools.runexample", None)
else:
init_contents = b"class not in\n"
self.package = fixtures.PythonPackage(
"runexample", [("__init__.py", init_contents)]
)

def setUp(self):
super().setUp()
self.useFixture(self.package)
testtools.__path__.append(self.package.base)
self.addCleanup(testtools.__path__.remove, self.package.base)
self.addCleanup(sys.modules.pop, "testtools.runexample", None)

if fixtures and testresources:

class SampleResourcedFixture(fixtures.Fixture):
"""Creates a test suite that uses testresources."""
class SampleResourcedFixture(fixtures.Fixture):
"""Creates a test suite that uses testresources."""

def __init__(self):
super().__init__()
self.package = fixtures.PythonPackage(
"resourceexample",
[
(
"__init__.py",
b"""
def __init__(self):
super().__init__()
self.package = fixtures.PythonPackage(
"resourceexample",
[
(
"__init__.py",
b"""
from fixtures import Fixture
from testresources import (
FixtureResource,
Expand Down Expand Up @@ -109,30 +97,28 @@ def test_suite():
from unittest import TestLoader
return OptimisingTestSuite(TestLoader().loadTestsFromName(__name__))
""",
)
],
)

def setUp(self):
super().setUp()
self.useFixture(self.package)
self.addCleanup(testtools.__path__.remove, self.package.base)
testtools.__path__.append(self.package.base)
)
],
)

def setUp(self):
super().setUp()
self.useFixture(self.package)
self.addCleanup(testtools.__path__.remove, self.package.base)
testtools.__path__.append(self.package.base)

if fixtures:

class SampleLoadTestsPackage(fixtures.Fixture):
"""Creates a test suite package using load_tests."""
class SampleLoadTestsPackage(fixtures.Fixture):
"""Creates a test suite package using load_tests."""

def __init__(self):
super().__init__()
self.package = fixtures.PythonPackage(
"discoverexample",
[
(
"__init__.py",
b"""
def __init__(self):
super().__init__()
self.package = fixtures.PythonPackage(
"discoverexample",
[
(
"__init__.py",
b"""
from testtools import TestCase, clone_test_with_new_id

class TestExample(TestCase):
Expand All @@ -143,22 +129,17 @@ def load_tests(loader, tests, pattern):
tests.addTest(clone_test_with_new_id(tests._tests[1]._tests[0], "fred"))
return tests
""",
)
],
)

def setUp(self):
super().setUp()
self.useFixture(self.package)
self.addCleanup(sys.path.remove, self.package.base)

)
],
)

class TestRun(TestCase):
def setUp(self):
super().setUp()
if fixtures is None:
self.skipTest("Need fixtures")
self.useFixture(self.package)
self.addCleanup(sys.path.remove, self.package.base)


class TestRun(TestCase):
def test_run_custom_list(self):
self.useFixture(SampleTestFixture())
tests = []
Expand Down Expand Up @@ -347,8 +328,6 @@ def test_run_load_list(self):
)

def test_load_list_preserves_custom_suites(self):
if testresources is None:
self.skipTest("Need testresources")
self.useFixture(SampleResourcedFixture())
# We load two tests, not loading one. Both share a resource, so we
# should see just one resource setup occur.
Expand Down
12 changes: 2 additions & 10 deletions tests/test_testresult.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
from typing import Any
from unittest import TestSuite

import testresources

from testtools import (
CopyStreamResult,
ExtendedToOriginalDecorator,
Expand Down Expand Up @@ -84,11 +86,6 @@
run_with_stack_hidden,
)

try:
import testresources
except ImportError:
testresources = None


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


class TestResourcedToStreamDecorator(TestCase):
def setUp(self):
super().setUp()
if testresources is None:
self.skipTest("Need testresources")

def test_startMakeResource(self):
log = LoggingStreamResult()
result = ResourcedToStreamDecorator(log)
Expand Down
12 changes: 2 additions & 10 deletions tests/test_testsuite.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import unittest
from pprint import pformat

from fixtures import FunctionFixture

from testtools import (
ConcurrentStreamTestSuite,
ConcurrentTestSuite,
Expand All @@ -20,11 +22,6 @@

from .helpers import LoggingResult

try:
from fixtures import FunctionFixture
except ImportError:
FunctionFixture = None # type: ignore


class Sample(TestCase):
def __hash__(self):
Expand Down Expand Up @@ -348,11 +345,6 @@ def test_simple(self):


class TestFixtureSuite(TestCase):
def setUp(self):
super().setUp()
if FunctionFixture is None:
self.skipTest("Need fixtures")

def test_fixture_suite(self):
log: list[int | str] = []

Expand Down