Skip to content
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

Replace source code: <package>/test/__init__.py and run.py #51

Closed
wants to merge 2 commits into from
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env python
##############################################################################
#
# (c) {% now 'utc', '%Y' %} The Trustees of Columbia University in the City of New York.
# All rights reserved.
#
# File coded by: Billinge Group members and community contributors.
#
# See GitHub contributions for a more detailed list of contributors.
# https://github.com/{{ cookiecutter.github_org }}/{{ cookiecutter.repo_name }}/graphs/contributors
#
# See LICENSE.rst for license information.
#
##############################################################################

"""Unit tests for {{ cookiecutter.project_name }}.
"""

import unittest


def testsuite(pattern=""):
"""Create a unit tests suite for {{ cookiecutter.project_name }} package.

Parameters
----------
pattern : str, optional
Regular expression pattern for selecting test cases.
Select all tests when empty. Ignore the pattern when
any of unit test modules fails to import.

Returns
-------
suite : `unittest.TestSuite`
The TestSuite object containing the matching tests.
"""
import re
from importlib.resources import as_file, files
from itertools import chain
from os.path import dirname

loader = unittest.defaultTestLoader
ref = files(__package__)
with as_file(ref) as thisdir:
depth = __name__.count(".") + 1
topdir = thisdir
for i in range(depth):
topdir = dirname(topdir)
suite_all = loader.discover(str(thisdir), top_level_dir=topdir)
# always filter the suite by pattern to test-cover the selection code.
suite = unittest.TestSuite()
rx = re.compile(pattern)
tsuites = list(chain.from_iterable(suite_all))
tsok = all(isinstance(ts, unittest.TestSuite) for ts in tsuites)
if not tsok: # pragma: no cover
return suite_all
tcases = chain.from_iterable(tsuites)
for tc in tcases:
tcwords = tc.id().split(".")
shortname = ".".join(tcwords[-3:])
if rx.search(shortname):
suite.addTest(tc)
# verify all tests are found for an empty pattern.
assert pattern or suite_all.countTestCases() == suite.countTestCases()
return suite


def test():
"""Execute all unit tests for the {{ cookiecutter.project_name }} package.

Returns
-------
result : `unittest.TestResult`
"""
suite = testsuite()
runner = unittest.TextTestRunner()
result = runner.run(suite)
return result


# End of file
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env python
##############################################################################
#
# (c) {% now 'utc', '%Y' %} The Trustees of Columbia University in the City of New York.
# All rights reserved.
#
# File coded by: Billinge Group members and community contributors.
#
# See GitHub contributions for a more detailed list of contributors.
# https://github.com/{{ cookiecutter.github_org }}/{{ cookiecutter.repo_name }}/graphs/contributors
#
# See LICENSE.rst for license information.
#
##############################################################################

"""Convenience module for executing all unit tests with

python -m {{ cookiecutter.project_name }}.tests.run
"""

if __name__ == "__main__":
import sys

# show warnings by default
if not sys.warnoptions:
import os
import warnings

warnings.simplefilter("default")
# also affect subprocesses
os.environ["PYTHONWARNINGS"] = "default"
from {{cookiecutter.project_name}}.tests import test

# produce zero exit code for a successful test
sys.exit(not test().wasSuccessful())

# End of file