Skip to content

Commit

Permalink
Use unittest discover instead of nose.core (#810)
Browse files Browse the repository at this point in the history
  • Loading branch information
ievacerny authored and kitchoi committed Jul 8, 2020
1 parent 2bec2b2 commit d03a071
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 6 deletions.
15 changes: 10 additions & 5 deletions etstool.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,17 @@ def test(runtime, toolkit, environment):
parameters = get_parameters(runtime, toolkit, environment)
environ = environment_vars.get(toolkit, {}).copy()
environ['PYTHONUNBUFFERED'] = "1"

if toolkit == "wx":
environ["EXCLUDE_TESTS"] = "qt"
elif toolkit in {"pyqt", "pyqt5", "pyside", "pyside2"}:
environ["EXCLUDE_TESTS"] = "wx"
else:
environ["EXCLUDE_TESTS"] = "(wx|qt)"

commands = [
"edm run -e {environment} -- coverage run -p -m nose.core -v traitsui.tests --nologcapture"]
# extra tests for qt
if toolkit in {'pyqt', 'pyside', 'pyqt5'}:
commands.append(
"edm run -e {environment} -- coverage run -p -m nose.core -v traitsui.qt4.tests --nologcapture")
"edm run -e {environment} -- coverage run -p -m unittest discover -v traitsui"
]

# We run in a tempdir to avoid accidentally picking up wrong traitsui
# code from a local dir. We need to ensure a good .coveragerc is in
Expand Down
44 changes: 44 additions & 0 deletions traitsui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,47 @@
"pyside": ["pyside>=1.2", "pygments"],
"demo": ["configobj", "docutils"],
}


# ============================= Test Loader ==================================
def load_tests(loader, standard_tests, pattern):
""" Custom test loading function that enables test filtering using regex
exclusion pattern.
Parameters
----------
loader : unittest.TestLoader
The instance of test loader
standard_tests : unittest.TestSuite
Tests that would be loaded by default from this module (no tests)
pattern : str
An inclusion pattern used to match test files (test*.py default)
Returns
-------
filtered_package_tests : unittest.TestSuite
TestSuite representing all package tests that did not match specified
exclusion pattern.
"""
from os import environ
from os.path import dirname
from traitsui.tests._tools import filter_tests
from unittest import TestSuite

# Make sure the right toolkit is up and running before importing tests
from traitsui.toolkit import toolkit
toolkit()

this_dir = dirname(__file__)
package_tests = loader.discover(start_dir=this_dir, pattern=pattern)

exclusion_pattern = environ.get("EXCLUDE_TESTS", None)
if exclusion_pattern is None:
return package_tests

filtered_package_tests = TestSuite()
for test_suite in package_tests:
filtered_test_suite = filter_tests(test_suite, exclusion_pattern)
filtered_package_tests.addTest(filtered_test_suite)

return filtered_package_tests
20 changes: 19 additions & 1 deletion traitsui/tests/_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
# ------------------------------------------------------------------------------


import re
import sys
import traceback
from functools import partial
from contextlib import contextmanager
from unittest import skipIf
from unittest import skipIf, TestSuite

from pyface.toolkit import toolkit_object
from traits.etsconfig.api import ETSConfig
Expand Down Expand Up @@ -113,6 +114,23 @@ def wrapped(*args, **kwargs):
return wrapped


def filter_tests(test_suite, exclusion_pattern):
filtered_test_suite = TestSuite()
for item in test_suite:
if isinstance(item, TestSuite):
filtered = filter_tests(item, exclusion_pattern)
filtered_test_suite.addTest(filtered)
else:
match = re.search(exclusion_pattern, item.id())
if match is not None:
skip_msg = "Test excluded via pattern '{}'".format(
exclusion_pattern
)
setattr(item, 'setUp', lambda: item.skipTest(skip_msg))
filtered_test_suite.addTest(item)
return filtered_test_suite


# ######### Utility tools to test on both qt4 and wx


Expand Down

0 comments on commit d03a071

Please sign in to comment.