Skip to content

Commit

Permalink
Make aggregate_tests callable from project dir
Browse files Browse the repository at this point in the history
This is Python testing convention and allows coverage to correctly
record the paths of the source files and left-strip the parts
leading to the project directory.

This commit also replaces manual test loading with the unittest
`discover` function, which adds some path magic, so that tests
are actually run over the source code in the project dir.

For the sake of simplified test discovery, this commit removes
randomization of test module loading.
It also does not seem beneficial to just shuffle the test
modules and not the test cases or test methods.
  • Loading branch information
lukpueh committed Jun 27, 2019
1 parent dd9e146 commit 4c8bfaf
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 27 deletions.
2 changes: 2 additions & 0 deletions tests/.coveragerc → .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ exclude_lines =

omit =
*/securesystemslib/_vendor/*
*/tests/*
*/site-packages/*
3 changes: 0 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,4 @@ script:
- tox

after_success:
- cd tests
- coveralls
- cd -

24 changes: 2 additions & 22 deletions tests/aggregate_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<Purpose>
Run all the unit tests from every .py file beginning with "test_" in
'tuf/tests'. Use --random to run the tests in random order.
'securesystemslib/tests'.
"""

# Help with Python 3 compatibility, where the print statement is a function, an
Expand All @@ -33,29 +33,9 @@

import sys
import unittest
import glob
import random

# Generate a list of pathnames that match a pattern (i.e., that begin with
# 'test_' and end with '.py'. A shell-style wildcard is used with glob() to
# match desired filenames. All the tests matching the pattern will be loaded
# and run in a test suite.
tests_list = glob.glob('test_*.py')

# Remove '.py' from each filename to allow loadTestsFromNames() (called below)
# to properly load the file as a module.
tests_without_extension = []
for test in tests_list:
test = test[:-3]
tests_without_extension.append(test)

# Randomize the order in which the tests run. Randomization might catch errors
# with unit tests that do not properly clean up or restore monkey-patched
# modules.
random.shuffle(tests_without_extension)

if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromNames(tests_without_extension)
suite = unittest.TestLoader().discover("tests", top_level_dir=".")
all_tests_passed = unittest.TextTestRunner(verbosity=1).run(suite).wasSuccessful()
if not all_tests_passed:
sys.exit(1)
3 changes: 1 addition & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
envlist = py27, py34, py35, py36

[testenv]
changedir = tests

commands =
coverage run --source securesystemslib aggregate_tests.py
coverage run tests/aggregate_tests.py
coverage report -m --fail-under 100

deps =
Expand Down

0 comments on commit 4c8bfaf

Please sign in to comment.