问题
I want the following project structure:
|--folder/
| |--tests/
| |--project/
Let's write a simple example:
|--test_pytest/
| |--tests/
| | |--test_sum.py
| |--t_pytest/
| | |--sum.py
| | |--__init__.py
sum.py:
def my_sum(a, b):
return a + b
test_sum.py:
from t_pytest.sum import my_sum
def test_my_sum():
assert my_sum(2, 2) == 5, "math still works"
Let's run it:
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 =========
It can't see t_pytest module. It was made like httpie:
https://github.com/jkbrzt/httpie/
https://github.com/jkbrzt/httpie/blob/master/tests/test_errors.py
Why? How can I correct it?
回答1:
An alternative way is making tests a module as well, by adding __init__.py file in folder tests. One of the most popular python lib requests https://github.com/kennethreitz/requests is doing in this way in their unit tests folder tests. You don't have to export PYTHONPATH to your project to execute tests.
The limitation is you have to run py.test command in directory 'test_pytest' in your example project.
One more thing, the import line in your example code is wrong. It should be
from t_pytest.sum import my_sum
回答2:
Thank you, jonrsharpe. Py.test does no magic, I need to make my packages importable myself. There is one of possible solutions:
$ export PYTHONPATH="${PYTHONPATH}: [Path to folder with my module]"
( PYTHONPATH is one of path sources for sys.path )
If I need to make this change permanently, I need to add this string to ~/.bashrc
回答3:
Try this:
# in bash/zsh shell
test_pytest$ PYTHONPATH=. pytest
# in fish shell
test_pytest$ env PYTHONPATH=. pytest
It is the same as what @stepuncius points at, and what this answer refers to in a gist. I just wanted to put this in one clean answer. It is a simple solution for small projects, without meddling with the packet-module structure, or setup.py
, etc.
回答4:
With pytest:
if __name__ == '__main__':
import pytest
pytest.main(['-x', 'tests', '--capture', 'sys'])
# Note: tests is the directory name
This might be helpful for solving your problem with unittest:
Assume we have starter script called run_tests.py
the code in 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)
you just replace ./tests
into your toplevel target dir(absolute path) contains all test scripts
you just run this file like
python run_tests.py
you will be able to see running all the test scripts.
来源:https://stackoverflow.com/questions/37816820/pytest-how-to-make-dedicated-test-directory