How do I run trial so that it executes all tests within a directory? All my unit tests pass if I run trial on each file individually, but if I try something like...
trial test/
on the test directory, it gives me one "PASSED
", and the following message...
UserWarning: (for module __in开发者_StackOverflowit__) not in path importer cache (PEP 302 violation
- check your local configuration).
rather than actually running all the tests within the directory.
First of all: you can't call your top-level unit test package test
. That's the name of Python's unit tests, so you will never be able to run your tests in an installed configuration, and depending on how your python is set up, you may end up importing python's own tests instead of your own.
Second: sys.path
is a vast and subtle mystery.
trial
supports running on files and directories as a quick getting-started hack, but it can never really be completely correct about using path names. The right thing to do is to pass trial a module (or package) name, that it can import as a python module and inspect.
So if your directory structure looks like:
~/Projects/MyProject/
~/Projects/MyProject/myproject/
~/Projects/MyProject/myproject/__init__.py
~/Projects/MyProject/myproject/stuff.py
~/Projects/MyProject/myproject/test/
~/Projects/MyProject/myproject/test/__init__.py
~/Projects/MyProject/myproject/test/test_stuff.py
then you should run your tests like this:
PYTHONPATH=$HOME/Projects/MyProject (cd /tmp; trial myproject.test)
in other words, don't run your tests from within your project's directory; this dumps _trial_temp directories all over your source code, confuses "the place I load my code from" and "the current directory" and generally makes a muddle of various things which can be difficult to untangle later.
So, set up your PYTHONPATH
and PATH
using the path-management tool of your choice: Combinator, setup.py develop, virtualenv – or just dumping junk into your ~/.bashrc
– and then run trial
from some temporary location, on a uniquely-named top-level Python package, and everything should work just fine.
精彩评论