运行试验#

GEvent有一个广泛的回归测试套件,使用标准实现 unittest 模块。它使用 custom testrunner 它提供了增强的测试隔离(对猴子补丁很重要),并行运行测试,并处理其他特定于GEvent的异常。

备注

gevent测试过程运行Python标准库测试,并应用gevent的monkey补丁,以确保gevent行为正确(与标准库匹配)。标准库 test 必须可用才能执行此操作。

这种情况通常是自动发生的,但有些发行版会删除此模块。值得注意的是,在Debian上,您可能需要 libpythonX.Y-testsuite 已安装以运行所有测试。

测试运行程序有许多选项:

$ python -mgevent.tests --help
usage: __main__.py [-h] [--ignore IGNORE] [--discover] [--config CONFIG]
                   [--coverage] [--quiet] [--verbose] [--debug]
                   [--package PACKAGE] [--processes PROCESSES] [--no-combine]
                   [-u RES1,RES2,...] [--travis-fold MSG]
                   [--second-chance | --failfast]
                   [tests ...]

positional arguments:
  tests

options:
  -h, --help            show this help message and exit
  --ignore IGNORE
  --discover            Only print the tests found.
  --config CONFIG       The path to the config file containing FAILING_TESTS,
                        IGNORED_TESTS and RUN_ALONE. Defaults to
                        known_failures.py.
  --coverage            Enable coverage recording with coverage.py.
  --quiet               Be quiet. Defaults to True. Also the GEVENTTEST_QUIET
                        environment variable.
  --verbose
  --debug               Enable debug settings. If the GEVENT_DEBUG environment
                        variable is not set, this sets it to 'debug'. This can
                        also enable PYTHONTRACEMALLOC and the debug
                        PYTHONMALLOC allocators, if not already set. Defaults
                        to False.
  --package PACKAGE     Load tests from the given package. Defaults to
                        gevent.tests.
  --processes PROCESSES, -j PROCESSES
                        Use up to the given number of parallel processes to
                        execute tests. Defaults to 4.
  --no-combine          Do not combine tests into process groups.
  -u RES1,RES2,..., --use RES1,RES2,...
                        specify which special resource intensive tests to run.
                        "all" is the default; "none" may also be used. Disable
                        individual resources with a leading -.For example,
                        "-u-network". GEVENTTEST_USE_RESOURCES is used if no
                        argument is given. To only use one resources, specify
                        "-unone,resource".
  --travis-fold MSG     Emit Travis CI log fold markers around the output.
  --second-chance       Give failed tests a second chance.
  --failfast, -x        Stop running after the first failure.

运行所有测试的最简单方法就是调用测试运行程序,通常是从源代码签出的根目录调用:

(gevent-env) $ python -mgevent.tests
Running tests in parallel with concurrency 7
...
Ran 3107 tests (skipped=333) in 132 files in 01:52

您还可以使用测试运行程序运行单个gevent测试文件:

(gevent-env) $ python -m gevent.tests test__util.py
Running tests in parallel with concurrency 1
+ /.../python -u -mgevent.tests.test__util
- /.../python -u -mgevent.tests.test__util [Ran 9 tests in 1.1s]

Longest-running tests:
1.1 seconds: /.../python -u -mgevent.tests.test__util

Ran 9 tests in 1 files in 1.1s

或者你可以运行一个猴子补丁的标准库测试:

(gevent-env) $ python -m gevent.tests.test___monkey_patching test_socket.py
Running tests in parallel with concurrency 1
+ /.../python -u -W ignore -m gevent.testing.monkey_test test_socket.py
Running with patch_all(Event=False): test_socket.py
Added imports 1
Skipped testEmptyFileSend (1)
...
Ran 555 tests in 23.042s

OK (skipped=172)
- /.../python -u -W ignore -m gevent.testing.monkey_test test_socket.py [took 26.7s]

Longest-running tests:
26.7 seconds: /.../python -u -W ignore -m gevent.testing.monkey_test test_socket.py

Ran 0 tests in 1 files in 00:27

环境变量#

一些TestRunner选项具有等效的环境变量。尤其是, --quietGEVENTTEST_QUIET-uGEVENTTEST_USE_RESOURCES .

使用托克斯#

在提交拉请求之前,最好在所有受支持的Python版本上运行测试,并使用prospector/pylint检查代码质量。这就是在CI上执行的操作。在本地,可以使用TOX::

pip install tox
tox

测量代码覆盖率#

这是在CI上完成的,因此通常不需要在本地完成。

测试运行程序接受 --coverage 参数以通过 coverage.py 包裹。就像这样:

python -m gevent.tests --coverage
coverage combine
coverage html -i
<open htmlcov/index.html>

限制资源使用#

gevent支持标准库测试套件的资源。默认情况下启用所有资源。禁用资源将禁用使用这些资源的测试。例如,要禁用访问外部网络(Internet)的测试,请禁用 network 资源。可以选择:

$ python -m gevent.tests -u-network

环境变量:

$ GEVENTTEST_USE_RESOURCES=-network python -m gevent.tests