在本地运行 SciPy 测试#
在 Python 解释器中进行基本的测试编写和执行在 NumPy/SciPy 测试指南 中有记录。此页面包含有关使用 SciPy 的 dev.py
命令行工具从命令行运行测试的信息。注意:在开始之前,请确保已安装 pytest
。
注意
dev.py
接口是自文档化的,这意味着本页上的所有内容以及更多内容(包括每个命令的用法示例)都可以通过 python dev.py --help
访问,对于单个命令,例如 python dev.py <command-name> --help
。在这种情况下,您可以检查 python dev.py test --help
。
要运行所有测试,请在命令行中导航到根 SciPy 目录并执行
python dev.py test
这将构建 SciPy(或更新现有构建)并运行测试。
要对特定子模块(例如 optimize
)运行测试,请使用 --submodule
选项
python dev.py test -s optimize
要运行特定测试模块,请使用 Pytest 语法 --test
(或 -t
)
python dev.py test -t scipy.<module>.tests.<test_file>
例如,要运行 scipy/optimize/tests/test_linprog.py
文件中的测试,请运行
python dev.py test -t scipy.optimize.tests.test_linprog
要运行测试类
python dev.py test -t scipy.<module>.tests.<test_file>::<TestClass>
例如,要运行 test_linprog.py
中的 TestLinprogRSCommon
类
python dev.py test -t scipy.optimize.tests.test_linprog::TestLinprogRSCommon
要运行特定测试
python dev.py test -t scipy.<module>.tests.<test_file>::<test_name>
例如,要运行 test_linprog.py
中的 test_unknown_solvers_and_options
python dev.py test -t scipy.optimize.tests.test_linprog::test_unknown_solvers_and_options
对于类内的测试,您需要指定类名和测试名
python dev.py test -t scipy.<module>.tests.<test_file>::<TestClass>::<test_name>
例如
python dev.py test -t scipy.optimize.tests.test_linprog::TestLinprogRSCommon::test_nontrivial_problem_with_guess
其他有用的选项包括
-v
或--verbose
,它激活详细选项以获取更详细的输出。--coverage
用于在scipy/build/coverage/index.html
中生成测试覆盖率报告。注意:pytest-cov
必须安装。-n
或--no-build
用于阻止 SciPy 在测试之前更新构建-j
或--parallel
n 用于在构建 SciPy 时使用 n 个核心;例如,python dev.py test -j 4
使用四个核心。从 #10172 开始,如果安装了pytest-xdist
,它也会在四个核心上运行测试。-m
或--mode
full
用于运行完整的测试套件,包括慢速测试。例如,python dev.py test -m full
。--
用于将剩余的命令行参数发送给pytest
,而不是dev.py test
。例如,当发送给pytest.py
的-n
激活了--no-build
选项时,发送给pytest
的-n
会在多个核心上运行测试;例如,python dev.py test -- -n 4
使用四个核心运行测试。注意:pytest-xdist
必须安装才能在多个核心上进行测试。
有关 pytest
的更多信息,请参阅 pytest
文档。
提示:#
如果您从源代码构建了 SciPy,但在更改代码库后遇到运行测试的问题,请尝试删除 scipy/build
目录。这将强制 dev.py
在执行测试之前完全重建 SciPy。
还有一个级别非常慢的测试(几分钟),即使调用 python dev.py test -m full
也会被禁用。可以通过在运行测试套件之前设置环境变量 SCIPY_XSLOW=1
来启用它们。
默认情况下,使用 Hypothesis
的测试使用 scipy/scipy/conftest.py
中定义的 deterministic
配置文件。此配置文件包含 Hypothesis 设置 derandomize=True
,因此在 Hypothesis、Python 或测试函数更新之前,将使用相同的示例。为了更好地利用 Hypothesis 查找反例的能力,请在运行测试套件之前通过设置环境变量 SCIPY_HYPOTHESIS_PROFILE=nondeterministic
选择 nondeterministic
配置文件。运行的示例数量可以通过编辑选定的配置来配置,例如添加 max_examples=100_000
。