Skip to content

PYTHON-3887 Remove custom test command in setup.py #1350

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 10, 2023
Merged
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: 0 additions & 2 deletions .evergreen/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ createvirtualenv () {

python -m pip install --upgrade pip
python -m pip install --upgrade setuptools wheel tox
# lxml only has wheels for macos 10.15+
python -m pip install unittest-xml-reporting || true
}

# Usage:
Expand Down
3 changes: 0 additions & 3 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,3 @@ warn_unused_ignores = True

[mypy-winkerberos.*]
ignore_missing_imports = True

[mypy-xmlrunner.*]
ignore_missing_imports = True
66 changes: 2 additions & 64 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,71 +8,11 @@
except ImportError:
pass

from setuptools import Command, setup
from setuptools import setup
from setuptools.command.build_ext import build_ext
from setuptools.extension import Extension


class test(Command):
description = "run the tests"

user_options = [
("test-module=", "m", "Discover tests in specified module"),
("test-suite=", "s", "Test suite to run (e.g. 'some_module.test_suite')"),
("failfast", "f", "Stop running tests on first failure or error"),
("xunit-output=", "x", "Generate a results directory with XUnit XML format"),
]

def initialize_options(self):
self.test_module = None
self.test_suite = None
self.failfast = False
self.xunit_output = None

def finalize_options(self):
if self.test_suite is None and self.test_module is None:
self.test_module = "test"
elif self.test_module is not None and self.test_suite is not None:
raise Exception("You may specify a module or suite, but not both")

def run(self):
# Installing required packages, running egg_info and build_ext are
# part of normal operation for setuptools.command.test.test
if self.distribution.install_requires:
self.distribution.fetch_build_eggs(self.distribution.install_requires)
if self.distribution.tests_require:
self.distribution.fetch_build_eggs(self.distribution.tests_require)
if self.xunit_output:
self.distribution.fetch_build_eggs(["unittest-xml-reporting"])
self.run_command("egg_info")
build_ext_cmd = self.reinitialize_command("build_ext")
build_ext_cmd.inplace = 1
self.run_command("build_ext")

# Construct a TextTestRunner directly from the unittest imported from
# test, which creates a TestResult that supports the 'addSkip' method.
# setuptools will by default create a TextTestRunner that uses the old
# TestResult class.
from test import PymongoTestRunner, test_cases, unittest

if self.test_suite is None:
all_tests = unittest.defaultTestLoader.discover(self.test_module)
suite = unittest.TestSuite()
suite.addTests(sorted(test_cases(all_tests), key=lambda x: x.__module__))
else:
suite = unittest.defaultTestLoader.loadTestsFromName(self.test_suite)
if self.xunit_output:
from test import PymongoXMLTestRunner

runner = PymongoXMLTestRunner(
verbosity=2, failfast=self.failfast, output=self.xunit_output
)
else:
runner = PymongoTestRunner(verbosity=2, failfast=self.failfast)
result = runner.run(suite)
sys.exit(not result.wasSuccessful())


class custom_build_ext(build_ext):
"""Allow C extension building to fail.

Expand Down Expand Up @@ -189,6 +129,4 @@ def build_extension(self, ext):
)
ext_modules = []

setup(
cmdclass={"build_ext": custom_build_ext, "test": test}, ext_modules=ext_modules
) # type:ignore
setup(cmdclass={"build_ext": custom_build_ext}, ext_modules=ext_modules) # type:ignore
26 changes: 0 additions & 26 deletions test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,6 @@
import unittest
import warnings

try:
from xmlrunner import XMLTestRunner

HAVE_XML = True
# ValueError is raised when version 3+ is installed on Jython 2.7.
except (ImportError, ValueError):
HAVE_XML = False

try:
import ipaddress # noqa

Expand Down Expand Up @@ -1240,24 +1232,6 @@ def teardown():
print_running_clients()


class PymongoTestRunner(unittest.TextTestRunner):
def run(self, test):
setup()
result = super().run(test)
teardown()
return result


if HAVE_XML:

class PymongoXMLTestRunner(XMLTestRunner): # type: ignore[misc]
def run(self, test):
setup()
result = super().run(test)
teardown()
return result


def test_cases(suite):
"""Iterator over all TestCases within a TestSuite."""
for suite_or_case in suite._tests:
Expand Down