I decided to give it a go.
Virtualenv is up (python 3.5.2) pip install -U pytest (according to: https://docs.pytest.org/en/latest/getting-started.html).
Next documentation says I should run:
$ pytest --version
This is pytest version 3.x.y, imported from $PYTHON_PREFIX/lib/python3.6/site-packages/pytest.py
But this is actually what I got:
$ pytest --version
Usage: pytest [OPTIONS] [testfile [testpattern]]
examples:
pytest path/to/mytests.py
pytest path/to/mytests.py TheseTests
pytest path/to/mytests.py TheseTests.test_thisone
pytest path/to/mytests.py -m '(not long and database) or regr'
pytest one (will run both test_thisone and test_thatone)
pytest path/to/mytests.py -s not (will skip test_notthisone)
pytest: error: no such option: --version
But if I run:
py.test --version
This is pytest version 3.9.1, imported from ${HOME}/${V_ENV}/lib/python3.5/site-packages/pytest.py
According to that answer:
"py.test" vs "pytest" command
py.test is old (deprecated they say) and pytest is way to go.
I checked both of them:
$ which pytest
$HOME/$V_ENV/bin/pytest
$ which py.test
$HOME/$V_ENV/bin/py.test
Exactly same files.
$HOME is my home, but $V_ENV is where I keep my virutalenvs (I use virtualenvwrapper).
When running tests:
It works:
$ py.test tests/
It does not (exception):
$ pytest tests/
I checked stack trace.
py.test runs using python3 (correct)
pytest runs using python2 (incorrect, it's from os)
Anyone knows what is going on?
Also looks like py.test and pytest are exactly the same, so this notion of not using py.test seems a bit obsolete. Am I correct in saying that?
As it happens py is installed in system python libraries (as probably yet another dependency of something). There is a consistent way to to reproduce the bug. It involves following steps.
create virtualenv
run pytest --version (it will complain that pytest has no --version)
pip install pytest
run pytest --version (it will complain that pytest has no --version)
And it will stay this way. However if you deactivate and activate env or
outright close shell and reopen it, it will fix it.
=== Old Anwer ===
Thanks to #hoefling, he nudged me correct direction!
There is an old library called py that is installed as dependency of tox. It looks like that under certain circumstances related to installation and/or running those two libraries you may end up with corrupted install. Initially I thought it was outright order of installation (install tox first then pytest second) but I was unable to reproduce that. So current working theory is that I run tox first causing it to create invalid virtualenv and then I have installed pytest that got corrupted PYTHONPATH or PATH.
In short. There is a way to corrupt with tox your pytest, if it happens start from scratch people!
Related
I'm running a pytest test with cmd (from a .bat file) and getting a ModuleNotFoundError.
But when running it from PyCharm, it works fine.
I looked around and saw some solutions regarding PYTHONPATH but that doesn't seem to work.
Any idea how to solve this?
Thanks
Have you tried running:
$ python -m pytest /test/...
Instead of $ pytest /test/... you can find more on it in pytest docs
I would like to be able to run the tests suite when "mypackage" has been installed via pip.
I have created the mypackage-version.tar.gz file myself, using python setup.py sdist. mypackage-version.tar.gz does contain the tests/ directory (moreover, the .egg-info/SOURCES.txt inside it does list all files under tests/ too).
But after I've run pip install mypackage-version.tar.gz inside a dedicated brand new virtual environment, there is no tests/ directory anywhere in the virtual environment:
$ find ./myvenv/ -name "*tests*"
./myvenv/lib/python3.6/site-packages/pip/_vendor/webencodings/__pycache__/tests.cpython-36.pyc
./myvenv/lib/python3.6/site-packages/pip/_vendor/webencodings/tests.py
Reading pip documentation or the output of pip install --help, I just can't figure out if there's any way to ask pip to install the tests correctly along the rest.
I've also tried to unpack the tests/ directory manually (along with pytest.ini), but the tests do not start because pytest complains about not finding mypackage module (ModuleNotFoundError: No module named 'mypackage'), even if both pytest and mypackage show up in pip list... and both are the ones from the virtual environment (as which command tells), as well as pip and python (that can import mypackage).
Yet manually unpacking looks hacky, so before digging further into this, I'd liked to know what is the right way to go: using pip? Manually unpacking? Anything else? Am I missing something obvious?
EDIT: finally, I could run the manually unpacked tests using python -m pytest. But this remains a workaround, and I'd still liked to know whether there's a more proper (and automated) way to install and run the tests suite
I have Python package with setup.py. It has regular dependencies declared in install_requires and development dependencies declared in tests_require, e.g. flake8.
I thought pip install -e . or running python setup.py test will also install my development dependencies and they'll be available. However, apparently they're not and I struggle to setup my Travis CI build right.
install:
- "pip install -e ."
script:
- "python setup.py test"
- "flake8"
Build configured as above will fail, because flake8 will not be found as a valid command. I also tried to invoke flake8 from inside of the python setup.py test command (via subprocess), but also without success.
Also I hate the fact that flake8 can't be easily made integral part of the python setup.py test command, but that's another story.
I prefer to keep most of the configuration in tox.ini and rely on it to install and run what is to be run. For testing I use pytest (the solution can be modified to use other testing frameworks easily).
Following files are used:
tox.ini: automates the test
.travis.yml: instructions for Travis
setup.py: installation script to install the package to test
test_requirements.txt: list of requirements for testing
tox.ini
[tox]
envlist = py{26,27,33,34}
[testenv]
commands =
py.test -sv tests []
deps =
-rtest-requirements.txt
.travis.yml
sudo: false
language: python
python:
- 2.6
- 2.7
- 3.3
- 3.4
install:
- pip install tox-travis
script:
- tox
test_requirements.txt
Just ordinary requirements file whith what ever you need in there (e.g. flake8, pytest and other dependencies)
You may see sample at https://github.com/vlcinsky/awslogs/tree/pbr-setup.py
The fact it uses there pbr, coverage and coverall is not relevant to my answer (it works with or without pbr).
The more direct answer is that pip install will not install tests_require, intentionally separating runtime requirements from test requirements. python setup.py test creates a virtualenv-like environment to run the tests in, un-doing this afterwards. flake8 is thus unavailable once it is done.
Flake8 has setuptools integration and also integrates with pytest if you use that. pytest itself also integrates with setuptools.
I installed Nose on a Mac OSX 10.10.5 with Python2.7.9 using easy_install. The installation appeared to be successful:
Collecting nose
Downloading nose-1.3.7-py2-none-any.whl (154kB)
100% |████████████████████████████████| 155kB 2.3MB/s
Installing collected packages: nose
Successfully installed nose-1.3.7
But now, when I try even basic stuff with nosetests on the command line, like nosetests -h or which nosetests I just get:
bash: nosetests: command not found
I have tried uninstalling, reinstalling using pip, tried installing with sudo and then running sudo nostests in the directories with tests scripts as other posts have suggested, but nothing seems to work.
The original purpose for installing was to use nose to run some basic tests with tests scripts I had written for these simple web.py apps. But nothing works, just keep getting the command not found response.
What's strange is that, when I open up the Python interpreter in Terminal, and do something like:
import nose
nose.main()
I get the expected result of:
.
----------------------------------------------------------------------
Ran 1 test in 0.135s
OK
So clearly it's installed....somewhere. Any suggestions for what the hell is going on here?
There are lots of error occurred when using pip install packages on Mac OS. So I recommend you install nose using easy_install.
$ pip uninstall nose
$ sudo easy_install nose
Then you can try nosetests now :)
I had this exact issue on OS X EI Captain with Python 2.7.10.
First I installed nose using pip:
$sudo pip install nose
which failed on the first attempt. Went through on the second attempt. But the nosetests command didn't work.
In order to fix this:
Step 1: Don't uninstall nose if it was installed already using pip as in my case.
Step 2:
$cd /usr/bin
$sudo easy_install nose
Above command finds the nosetests script (which was installed by pip earlier) & sets it under /usr/local/bin
Step 3: Try nosetests
$nosetests
----------------------------------------------------------------------
Ran 0 tests in 0.047s
OK
On UNIX-like systems like OS X, the script should be in /usr/local/bin. Make sure that directory is in the PATH environment variable in the shell that you use.
If not, you can also locate it using find, e.g:
find / -type f -name 'nosetests*' -perm +111 -print -quit
This means; search for a file whose name starts with nosetests, which has execute permissions set. Print the path name and stop.
I found that going to
Library/usr/bin
and running
sudo easy_install nose
it seems that sometimes it doesn't automatically install nose (and therefore nosetests functionality). Do the above lines, and you should be a-ok.
I wish i had a better explanation for why this happened, but i'm still pretty new, myself.
I had to use
Nosetest
or
python3 -m "nose"
Apparently this is the way Nosetest should be used in Python3. See also
How to make nosetests use python3
First, can you run 'python' from the command line? nosetests should be in that same directory:
rich bin $ which python
/home/rich/anaconda/bin/python
rich bin $ which nosetests
/home/rich/anaconda/bin/nosetests
It should also be in the downloaded nose package:
rich bin $ find /home/rich/anaconda -name nosetests
/home/rich/anaconda/pkgs/nose-1.3.3-py27_0/bin/nosetests
/home/rich/anaconda/pkgs/nose-1.3.7-py27_0/bin/nosetests
/home/rich/anaconda/bin/nosetests
From what I understand, everyone is moving to pytest - an actively-maintained testing framework.
It's not a solution to this problem, but it's likely the most-appropriate choice if you are still using nose.
I try to reinstall the pip, it doesn't work but lastly, when i use sudo ...it works
pip3 uninstall nose
sudo pip3 install nose
and
which nosetests
/usr/local/bin/nosetests
This can also happen if you were running nose within a virtual environment, and that virtual environment has been deactivated. If this is the case, reactivate with source bin/activate.
I have both Python 2.7 and 3.3 installed on a Mac. I'm trying to get Pytest to run my tests under Python 3.3.
When I run python3 –m py.test it halts, looking for the libs under the 3.3 path.
When I run pip install –U pytest it installs to the 2.7 path.
I've seen the write-ups for Virtualenv, but I'm not ready to go there yet.
Is there another way?
Take a look at the tox project. It helps you to test your code under different python versions and interpreters.
Example tox.ini config for your case:
[tox]
envlist = py27,py33
[testenv]
deps = pytest
commands = py.test
It's not a silver bullet but it will definitely help to automate your tedious test activities.
Apart from the genscript option the normal way to go about this is to intall py.test into the python3.3 interpreter's environment. The problem you have is that the pip you invoke is also a py27 version, so it will install into py27.
So you can start with installing pip into py33 (usually under the alias pip3) and then invoking that pip or you can simply install py and pytest in the py33 environment the old fashioned way: download the packages and run python3.3 setup.py install --user.
You will then still want to make sure you can invoke the correct version of py.test however, either making sure you can call py.test and py.test3 using aliases or so. Or simply by using pythonX.Y -m pytest.