From dc60f05a4057c870ab88ceb362e8e1c0d73048e2 Mon Sep 17 00:00:00 2001 From: Alex Willmer Date: Sun, 1 Apr 2018 01:42:31 +0100 Subject: [PATCH] tests: Switch to unit2 test runner, with coverage This means test files are imported as modules, not run as scripts. THey can still be run individually if so desired. Test coverage is measured, and an html report generated in htmlcov/. Test cases are automativally discovered, so they need not be listed twice. An overall passed/failed/skipped summary is printed, rather than for each file. Arguments passed to ./test are passed on to unit2. For instance ./test -v will print each test name as it is run. --- .gitignore | 2 ++ .travis.yml | 2 +- setup.cfg | 7 ++++++ test | 11 +++++++++ test.sh | 63 ------------------------------------------------- tests/README.md | 2 +- tox.ini | 2 +- 7 files changed, 23 insertions(+), 66 deletions(-) create mode 100755 test delete mode 100755 test.sh diff --git a/.gitignore b/.gitignore index 9a539564..9ec8ce9b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,9 @@ +.coverage .venv **/.DS_Store MANIFEST build/ dist/ docs/_build +htmlcov/ *.egg-info diff --git a/.travis.yml b/.travis.yml index a7f2f637..8deda0c0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,7 +12,7 @@ install: - pip install -r dev_requirements.txt script: -- MITOGEN_LOG_LEVEL=debug PYTHONPATH=. ${TRAVIS_BUILD_DIR}/test.sh +- MITOGEN_LOG_LEVEL=debug PYTHONPATH=. ${TRAVIS_BUILD_DIR}/test services: - docker diff --git a/setup.cfg b/setup.cfg index 44668df3..92051682 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,3 +1,10 @@ +[coverage:run] +branch = true +source = + mitogen +omit = + mitogen/compat/* + [flake8] ignore = E402,E128,W503 exclude = mitogen/compat diff --git a/test b/test new file mode 100755 index 00000000..aeb9c51c --- /dev/null +++ b/test @@ -0,0 +1,11 @@ +#/bin/sh + +UNIT2="$(which unit2)" + +coverage erase +coverage run "${UNIT2}" discover \ + --start-directory "tests" \ + --pattern '*_test.py' \ + "$@" +coverage html +echo coverage report is at "file://$(pwd)/htmlcov/index.html" diff --git a/test.sh b/test.sh deleted file mode 100755 index ab545104..00000000 --- a/test.sh +++ /dev/null @@ -1,63 +0,0 @@ -#!/bin/bash - -timeout() -{ - python -c ' -import subprocess -import sys -import time - -deadline = time.time() + float(sys.argv[1]) -proc = subprocess.Popen(sys.argv[2:]) -while time.time() < deadline and proc.poll() is None: - time.sleep(1.0) - -if proc.poll() is not None: - sys.exit(proc.returncode) -proc.terminate() -print -print >> sys.stderr, "Timeout! Command was:", sys.argv[2:] -print -sys.exit(1) - ' "$@" -} - -trap 'sigint' INT -sigint() -{ - echo "SIGINT received, stopping.." - exit 1 -} - -run_test() -{ - echo "Running $1.." - timeout 10 python $1 || fail=$? -} - -run_test tests/ansible_helpers_test.py -run_test tests/call_error_test.py -run_test tests/call_function_test.py -run_test tests/channel_test.py -run_test tests/fakessh_test.py -run_test tests/first_stage_test.py -run_test tests/fork_test.py -run_test tests/id_allocation_test.py -run_test tests/importer_test.py -run_test tests/latch_test.py -run_test tests/local_test.py -run_test tests/master_test.py -run_test tests/module_finder_test.py -run_test tests/nested_test.py -run_test tests/parent_test.py -run_test tests/receiver_test.py -run_test tests/responder_test.py -run_test tests/router_test.py -run_test tests/select_test.py -run_test tests/ssh_test.py -run_test tests/utils_test.py - -if [ "$fail" ]; then - echo "AT LEAST ONE TEST FAILED" >&2 - exit 1 -fi diff --git a/tests/README.md b/tests/README.md index 0ac4bcb1..41c024b5 100644 --- a/tests/README.md +++ b/tests/README.md @@ -27,4 +27,4 @@ and run the tests there. 1. Build the virtual environment ``virtualenv ../venv`` 1. Enable the virtual environment we just built ``source ../venv/bin/activate`` 1. Install Mitogen in pip editable mode ``pip install -e .`` -1. Run ``test.sh`` +1. Run ``test`` diff --git a/tox.ini b/tox.ini index f9eabeed..70de05df 100644 --- a/tox.ini +++ b/tox.ini @@ -8,7 +8,7 @@ deps = -r{toxinidir}/dev_requirements.txt commands = - {posargs:./test.sh} + {posargs:./test} [testenv:docs] basepython = python