Force py.test to use installed version of module - pytest

I have a mixed Python/C++ library with test files mixed in amongst source files in the same directories. The layout looks like
/home/irving/geode
geode
__init__.py
vector
__init__.py
test_vector.py
...
...
Unfortunately, the library is unusable in-place since it lacks .so extension modules. Question: Can I make py.test always use an installed version, even when run from /home/irving/geode or a subdirectory?
The test files have from __future__ import absolute_import, and run fine if executed directly as scripts. For example, if I do
cd geode/vector
./test_vector.py
which does import geode, it finds the installed version. However, if I run py.test in geode/vector, it finds the local copy of geode, and then dies.

I think you have two options:
run py.test --pyargs geode.vector.test_vector to make pytest interpretet the argument as an import path, deriving the file system path from it. This should run the test against the installed version.
move the tests out into a tests directory without an __init__.py file. This way you need to pip install -e . to work in-place or can do python setup.py install and the py.test tests to run tests against the installed version.

Related

pytest.ini doesn't take effect when calling pytest vs. pytest <test_name>

I am working creating some testing infrastructure and struggling with taking care of all the dependencies correctly.
The directory structure I have looks like:
conftest.py
kernels/
|-kernel_1/
|---<kernel_src1>
|---__init__.py
|---options.json
|---test/
|-----test_func1.py
|-kernel_2/
|---<kernel_src2>
|---__init__.py
|---pytest.ini
|---options.json
|---scripts/
|-----__init__.py
|-----some_module.py
|---test/
|-----test_func2.py
When I call pytest on any of these tests, the test first compiles and simulates the kernel source code (C++) and compares the output against golden that is generated in python. Since all the kernels will be compiled individually, I create an output directory to store compile/simulation logs along with some header files that we generated in the kernel_1 directory.
For example, pytest kernel_1/test/test_func1.py will create a directory in kernel_1/build_test_func1/<compile/sim logs>.
I use the conftest.py which updates cwd to the test directory based on the accepted answer here:
Change pytest working directory to test case directory
I also added pytest.ini to add kernel_2 to the pythonpath when running test_func2 so we can find modules in scripts folder:
[pytest]
pythonpath=.
Tests run correctly when calling it from:
cd kernel_2/; pytest
cd kernel_2/test; pytest
cd kernel_2; pytest test/test_func1.py
cd kernel_2/test; pytest test_func1.py
The test also runs correctly when calling it like this: pytest kernel_2/test/test_func2.py
But I start seeing ModuleImportError when calling it from top-level without specifying the test
pytest
ImportError while importing test module '<FULL_PATH>/kernel_2/test/test_func2.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
<FULL_PATH>miniconda3/envs/pytest/lib/python3.7/importlib/__init__.py:127: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
kernel_2/test/test_func2.py:8: in <module>
from scripts.some_module import some_func
E ModuleNotFoundError: No module named 'scripts'
The issue looks when collecting pytest.ini in a specific kernel doesn't take effect when calling pytest, but I haven't been able to find a way to fix this issue. Any comments, concerns are appreciated!

’unresolved import’ message for streamlit in Eclipse PyDev

I have successfully installed the streamlit package using the following
shell command and can run the resulting local server localhost:8501
Python3.8 -m pip install streamlit
In Eclipse, the module appears under the Package Library in the Python Interpreter.
I can import it as a library item in a PyDev module as as follows:
import streamlit
The only note I get from the compiler is that streamlit is an ‘unused import’. However, when I append the command as follows the compiler then says ‘unresloved import st’
import streamlit as st
Both ‘import streamlit’. and ‘import streamlit as st’ will not code complete.
How can I clear the ’unresolved import’ message ?
The sys.path is as follows:
/Users/davidklemitz/eclipse-workspace/streamlit
/Users/davidklemitz/eclipse-workspace/streamlit
/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8
/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/lib-dynload
/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages
/Library/Frameworks/Python.framework/Versions/3.8/lib/python38.zip
Thanks in advance for any help.
I solved the problem. Turns out it was a compound of two issues.
First the name of the PyDev module I chose was the same name as the package module name streamlit.py, installed using the following command
Python3.8 -m pip install streamlit
Second, I had a look at the PYTHONPATH under Eclipse->Preferences-> PyDev->Interpreters-> Python Interpreter where these paths were arranged as follows:
/Library/Frameworks/Python.framework/Versions/3.8/lib
/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages
As streamit.py was in the second and not the first, I reversed the order, restarted Eclipse, created a new PyDev project and associated development model named stream_lit.py and the code completed as expected.

Pytest can't find files/modules

I have had a look at several different topics on this matter but can't work out how to apply it to my situation. I don't have an init.py in my test folder and I have tried to use conftest. I have a directory structure like this:
--app
--app.py
--src
--init.py
--module1.py
--module2.py
--module3.py
--configs
--config.json
--non-default-config.json
--tests
--test1.py
--conftest.py
where app.py imports module1, which then imports modules 2&3 (using import src.module2). I load up config.json in all the modules files (and app.py) using:
with open('configs/config.json') as f:
CFG = json.load(f)
This works when I run app.py from the app directory. However, when I run pytest (which I believe should also be referencing from the app directory, since conftest.py is in the app directory) and it imports module1 (using import src.module1), it cannot find configs/config.json, but will find app/configs/config.json. I cannot use this as it will cause my app to break when I run app.py. However, Pytest can find the imports from within the src folder, even though this is on the same level as the configs folder.
If I move the conftest.py outside of the app directory and import module1 using import app.src.module1 then this import succeeds, but the import of module2 inside module1 then fails.
How can I resolve this issue? And is there a better way of structuring my project?
Solved this by running pytest from inside the app folder instead of from the base directory.

py.test "import file mismatch" despite different names (only Windows)

I am fairly new to py.test and have tried to set up a couple of simple black box tests for some legacy code. The directory structure looks somewhat like this:
X:\
conftest.py
prgm_A\
src\
test\
test_A.py
prgm_B\
src\
test\
test_B.py
When I run py.test from X:\, using py.test v. 2.6.3 in Windows 7 (or XP), py.test returns the following type of error message:
___________ ERROR collecting /prgm_A/test/test_A.py __________________
import file mismatch:
imported module 'test_A' has this __file__ attribute:
X:\prgm_A\test\test_A.py
which is not the same as the test file we want to collect:
X:\\prgm_A\test\test_A.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for
your test file modules
<and the same for B>
I have removed the __pycache__ and .pyc files, but that did not work. That extra backslash after the drive letter looks really fishy, but I am quite sure I am not to blame for that.
When I try to run the same tests in linux (despite the fact that the programs are compiled for windows), py.test v. 2.5.1 does not have the same problem.
My workaround until now has been to run the tests for each individual program from its own test directory, but after our computers were migrated to Windows 7, this stopped working.
Any ideas?
Additional Facts/Observations
I forgot to say that the tests used to work under XP, with an earlier py.test?, provided that I stepped down to X:\prgm_[AB]\test and ran py.test from there.
Superstition: Inserting one extra level in the file structure, moving everything from X:\ to X:\one_extra_level, didn't make one bit of a difference.
I have managed to reproduce the problem with this minimal example:
# conftest.py:
import pytest
def returns_xyz():
return "xyz"
#pytest.fixture(scope="session")
def provider():
"""Provides a subprogram which returns the string 'xyz'."""
return returns_xyz
# prgm_[AB]\test\test_[AB].py:
import pytest
def test_xyz(provider):
assert "xyz" == provider()
Everything you need for resolving this issue is to remove the python path file. It's a file with this extension .pyc and remove also the folder __pycache__ if it's available on your test or project.
Lamine
As explained here, you just have to add an __init__.py file in your test folder and it will do the trick.
the issue moved to https://github.com/pytest-dev/pytest/issues/702
this is not a bug, as far as i can tell the python module name is the same for both test modules
py.test complains about different files ending up with the same module name, thus breaking details
can you verify?
edit
its been verified as bug
Delete all .pyc file in your project directory
Execute Command : find . -name *.pyc -delete
It worked for me!

Can you run pychecker from virtualenv?

I want to do code analysis with pychecker but when it imports python code it doesn't use the packages from virtualenv, it uses the system wide one and the import fail.
Is there a way to install pychecker in a virtualenv or at least get it to just import the packages from the virtualenv?
Set your $PYTHONPATH environmental variable to the site-packages directory.
For me, pychecker was not able to find the configobj module. Since I use zsh (bash with lots of bells and whistles), I ran:
> find ~/venv-ops -name configobj.py
/Users/doug/venv-ops/lib/python2.7/site-packages/configobj.py
> export PYTHONPATH=/Users/doug/venv-ops/lib/python2.7/site-packages
and then pycheck worked fine.