pytest:如何制作专用测试目录
pytest: how to make dedicated test directory
我想要以下项目结构:
|--folder/
| |--tests/
| |--project/
我们来写一个简单的例子:
|--test_pytest/
| |--tests/
| | |--test_sum.py
| |--t_pytest/
| | |--sum.py
| | |--__init__.py
sum.py:
def my_sum(a, b):
return a + b
测试_sum.py:
from t_pytest.sum import my_sum
def test_my_sum():
assert my_sum(2, 2) == 5, "math still works"
让我们运行它:
test_pytest$ py.test ./
========== test session starts ===========
platform linux -- Python 3.4.3, pytest-2.9.2, py-1.4.31, pluggy-0.3.1
rootdir: /home/step/test_pytest, inifile:
collected 0 items / 1 errors
================= ERRORS =================
___ ERROR collecting tests/test_sum.py ___
tests/test_sum.py:1: in <module>
from t_pytest import my_sum
E ImportError: No module named 't_pytest'
======== 1 error in 0.01 seconds =========
它看不到 t_pytest 模块。它被制作成 httpie:
https://github.com/jkbrzt/httpie/
https://github.com/jkbrzt/httpie/blob/master/tests/test_errors.py
为什么?我该如何更正它?
谢谢你,乔恩夏普。 Py.test 没有任何魔力,我需要自己让我的包可以导入。有一种可能的解决方案:
$ export PYTHONPATH="${PYTHONPATH}: [Path to folder with my module]"
(PYTHONPATH 是 sys.path 的路径来源之一)
如果我需要永久进行此更改,我需要将此字符串添加到 ~/.bashrc
用pytest:
if __name__ == '__main__':
import pytest
pytest.main(['-x', 'tests', '--capture', 'sys'])
# Note: tests is the directory name
这可能有助于解决您的问题 with unittest:
假设我们有名为 run_tests.py
的起始脚本
run_tests.py
中的代码:
import unittest
if __name__ == '__main__':
# use the default shared TestLoader instance
test_loader = unittest.defaultTestLoader
# use the basic test runner that outputs to sys.stderr
test_runner = unittest.TextTestRunner()
# automatically discover all tests in the current dir of the form test*.py
# NOTE: only works for python 2.7 and later
test_suite = test_loader.discover('./tests')
# run the test suite
test_runner.run(test_suite)
你只需将 ./tests
替换为你的顶级目标目录(绝对路径)包含所有测试脚本
你只是运行这个文件喜欢
python run_tests.py
您将能够看到 运行ning 所有测试脚本。
另一种方法是通过在文件夹测试中添加 __init__.py 文件,使测试也成为一个模块。最流行的 python 库请求之一 https://github.com/kennethreitz/requests 就是在他们的单元测试文件夹测试中以这种方式进行的。您不必将 PYTHONPATH 导出到您的项目即可执行测试。
限制是您必须在示例项目的目录 'test_pytest' 中执行 运行 py.test 命令。
还有一点,您的示例代码中的导入行是错误的。应该是
from t_pytest.sum import my_sum
试试这个:
# in bash/zsh shell
test_pytest$ PYTHONPATH=. pytest
# in fish shell
test_pytest$ env PYTHONPATH=. pytest
和@stepuncius , and what this answer refers to in a gist一样。我只是想把这个放在一个干净的答案中。它是小型项目的简单解决方案,无需干预包模块结构或 setup.py
等
我想要以下项目结构:
|--folder/
| |--tests/
| |--project/
我们来写一个简单的例子:
|--test_pytest/
| |--tests/
| | |--test_sum.py
| |--t_pytest/
| | |--sum.py
| | |--__init__.py
sum.py:
def my_sum(a, b):
return a + b
测试_sum.py:
from t_pytest.sum import my_sum
def test_my_sum():
assert my_sum(2, 2) == 5, "math still works"
让我们运行它:
test_pytest$ py.test ./
========== test session starts ===========
platform linux -- Python 3.4.3, pytest-2.9.2, py-1.4.31, pluggy-0.3.1
rootdir: /home/step/test_pytest, inifile:
collected 0 items / 1 errors
================= ERRORS =================
___ ERROR collecting tests/test_sum.py ___
tests/test_sum.py:1: in <module>
from t_pytest import my_sum
E ImportError: No module named 't_pytest'
======== 1 error in 0.01 seconds =========
它看不到 t_pytest 模块。它被制作成 httpie:
https://github.com/jkbrzt/httpie/
https://github.com/jkbrzt/httpie/blob/master/tests/test_errors.py
为什么?我该如何更正它?
谢谢你,乔恩夏普。 Py.test 没有任何魔力,我需要自己让我的包可以导入。有一种可能的解决方案:
$ export PYTHONPATH="${PYTHONPATH}: [Path to folder with my module]"
(PYTHONPATH 是 sys.path 的路径来源之一)
如果我需要永久进行此更改,我需要将此字符串添加到 ~/.bashrc
用pytest:
if __name__ == '__main__':
import pytest
pytest.main(['-x', 'tests', '--capture', 'sys'])
# Note: tests is the directory name
这可能有助于解决您的问题 with unittest:
假设我们有名为 run_tests.py
run_tests.py
中的代码:
import unittest
if __name__ == '__main__':
# use the default shared TestLoader instance
test_loader = unittest.defaultTestLoader
# use the basic test runner that outputs to sys.stderr
test_runner = unittest.TextTestRunner()
# automatically discover all tests in the current dir of the form test*.py
# NOTE: only works for python 2.7 and later
test_suite = test_loader.discover('./tests')
# run the test suite
test_runner.run(test_suite)
你只需将 ./tests
替换为你的顶级目标目录(绝对路径)包含所有测试脚本
你只是运行这个文件喜欢
python run_tests.py
您将能够看到 运行ning 所有测试脚本。
另一种方法是通过在文件夹测试中添加 __init__.py 文件,使测试也成为一个模块。最流行的 python 库请求之一 https://github.com/kennethreitz/requests 就是在他们的单元测试文件夹测试中以这种方式进行的。您不必将 PYTHONPATH 导出到您的项目即可执行测试。
限制是您必须在示例项目的目录 'test_pytest' 中执行 运行 py.test 命令。
还有一点,您的示例代码中的导入行是错误的。应该是
from t_pytest.sum import my_sum
试试这个:
# in bash/zsh shell
test_pytest$ PYTHONPATH=. pytest
# in fish shell
test_pytest$ env PYTHONPATH=. pytest
和@stepuncius setup.py
等