Hello. I use pytest on a CI server to test our software. We have been using pytest for years but with the latest update we have an error importing a module during pytest. My logic behind isolating this issue to pytest is: on the CI server, nothing has changed (same python, same code, same OS image) the updated version of pytest is the only thing that has changed.
original versions of what pytest passed with
platform linux2 -- Python 2.7.12, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
current versions where I get failures:
platform linux2 -- Python 2.7.12, pytest-3.7.1, py-1.5.4, pluggy-0.7.1
Let's say the module that cannot import is called X. When within the directory of X, (where the _UnitTest directory is) running pytest allows all of the tests to pass. Changing directory back one layer (or anywhere outside of the X directory), now those tests from module X fail because the import statement failed.
Also on macOS I had all of the tests passing with pytest 3.6.3. Upgraded pytest, and now the tests related to module X fail. Many other modules import and work however module X does not. I have debugged a lot of it, and all of the paths look good, but it still fails to import. I'm not exactly sure what sets module X apart from the other dozens of module we use successfully.
I hope this makes sense and I can elaborate more if needed. I am not working in a virtualenv but I can put full pip lists in the comments if more is needed other then the versions I gave above.
Thank you
Adam
Same issue for me, if I downgrade to version <7.1 it's working.
@Agi-dev thank you for the feedback! glad to know i'm not alone
Hi guys, sorry for the delay.
Does 3.7.0 work for you? It might help pin point the problem. Also if any of you can produce a reproducible use case it would be great.
No worries. 3.7.0 does NOT work either. However, version 3.6.4 DOES work. I'm honestly very unsure how to reproduce this outside of my example. I can think about this at work tomorrow though. I'm not sure why it only happens to one of my modules out dozens.
I do know that it stems from my __init__ file in that module. That is where the import statement is, which is where the error occurs. (all of my other modules that pass have an __init__ like this as well)
@Agi-dev do you have any method to reproduce?
Sorry, I've tried to reproduce it with simple example with no success despite the fact I've found a way to make my tests work again.
I've fixed the pb (for me) by deleting a __init__.py in the first sub directory of my tests directory. I'm not sure it's the way to go, I'm relatively new in Python programming.
I have tried doing that to no luck :(
We should release 3.7.2 soon (probably next week) with a few fixes in place regarding package collection (#3768, #3526), let's see if they end up solving this issue as well.
Sounds great
Here is a way to reproduce the problem:
Make a directory structure such as
/tmp/dir1/
/tmp/dir1/__init__.py
/tmp/dir1/dir1_1/
/tmp/dir1/dir1_1/__init__.py
/tmp/dir1/dir1_1/test_1_1.py
/tmp/dir1/dir1_1/test_1_1.py::test_1_1_case
/tmp/dir1/dir1_2/
/tmp/dir1/dir1_2/test_1_2.py
/tmp/dir1/dir1_2/test_1_2.py::test_1_2_case
then run pytest /tmp --collect-only
the output is
================================================ test session starts =================================================
platform linux -- Python 3.5.2, pytest-3.7.1, py-1.5.4, pluggy-0.7.1
rootdir: /, inifile:
collected 2 items
<Package '/tmp/dir1'>
<Package '/tmp/dir1/dir1_1'>
<Module 'test_1_1.py'>
<Function 'test_1_1_case'>
<Module 'test_1_2.py'>
<Function 'test_1_2_case'>
It seems there is a problem when mixing directories of tests with packages of tests. Note that there is no mention of dir1_2 in the output. I saw this starting with 3.7.0. I do not see the issue with 3.6.4.
Eric
Thank you! That's great
Seems like a duplicate of #3768 . Can you try my pull request and see if that fixes your issue?
yea i can do that today
@jonozzz assuming i've followed your PR trail correctly, I should be cloning your fork of pytest-dev with this branch?
https://github.com/jonozzz/pytest/tree/fix-3768
Just installed...trying now.
Update: I still get the error
I would install it in a virtualenv like this:
pip install git+git://github.com/jonozzz/pytest.git@fix-3768
yea that's pretty much what I did. no luck though, same error
@adamhrosenberg might be a different issue then. Can you work on a minimal set of repro steps that you could share? Or at least paste the command you're running and the complete traceback and maybe I can figure out what's going on.
Not sure why this was closed, the PR still doesn't fix my problem. Sorry I wasn't able to get back to you right away it was a busy week. I'll post the traceback below.
I run a normal pytest, and I'm told that the module radiation_pressure cannot be found. It gets to the __init__ file which has from radiation_pressure import *. And it bugs out there. I posted the link to our software above.
Hi @adamhrosenberg, if this is still is an issue we can re-open this, no problem! Did you try out with the recently released 3.7.2?
I think this has more to do with PR #3797 because __init__.py is hardcoded in python_files. It will error out when there's a bad __init__.py somewhere, whereas in 3.6.x it'd just ignore it.
@adamhrosenberg can you post that traceback, hopefully that'll give me some clues on what's going on.
I am also encountering a similar sounding issue on 3.7.2. I'll try to figure out a reduced test case.
3.7.2 has fixed the issue for me.
Need a help...( I am very new to python)
I am not getting pytest.assume
I have imported
import pytest
and trying to use pytest.assume as mentioned in the doc, unable to use it
Can someone help me out..
@GiriManas pytest-assume works as expected.
$ cat test-assume.py
import pytest
@pytest.mark.parametrize(('x', 'y'), [(1, 1), (1, 0), (0, 1)])
def test_simple_assume(x, y):
pytest.assume(x == y)
pytest.assume(True)
pytest.assume(False)
$ sudo docker run -ti --rm -v "$(pwd)/test-assume.py:/root/test-assume.py" python:3 bash -c "pip install pytest-assume > /
dev/null 2>&1 && cd /root && ls && pytest test-assume.py"
test-assume.py
================================================================ test session starts =================================================================
platform linux -- Python 3.7.4, pytest-5.1.1, py-1.8.0, pluggy-0.12.0
rootdir: /root
plugins: assume-2.2.0
collected 3 items
test-assume.py FFF [100%]
====================================================================== FAILURES ======================================================================
______________________________________________________________ test_simple_assume[1-1] _______________________________________________________________
tp = <class 'pytest_assume.plugin.FailedAssumption'>, value = None, tb = None
def reraise(tp, value, tb=None):
try:
if value is None:
value = tp()
if value.__traceback__ is not tb:
> raise value.with_traceback(tb)
E pytest_assume.plugin.FailedAssumption:
E 1 Failed Assumptions:
E
E test-assume.py:7: AssumptionFailure
E >> pytest.assume(False)
E AssertionError: assert False
/usr/local/lib/python3.7/site-packages/six.py:692: FailedAssumption
______________________________________________________________ test_simple_assume[1-0] _______________________________________________________________
tp = <class 'pytest_assume.plugin.FailedAssumption'>, value = None, tb = None
def reraise(tp, value, tb=None):
try:
if value is None:
value = tp()
if value.__traceback__ is not tb:
> raise value.with_traceback(tb)
E pytest_assume.plugin.FailedAssumption:
E 2 Failed Assumptions:
E
E test-assume.py:5: AssumptionFailure
E >> pytest.assume(x == y)
E AssertionError: assert False
E
E test-assume.py:7: AssumptionFailure
E >> pytest.assume(False)
E AssertionError: assert False
/usr/local/lib/python3.7/site-packages/six.py:692: FailedAssumption
______________________________________________________________ test_simple_assume[0-1] _______________________________________________________________
tp = <class 'pytest_assume.plugin.FailedAssumption'>, value = None, tb = None
def reraise(tp, value, tb=None):
try:
if value is None:
value = tp()
if value.__traceback__ is not tb:
> raise value.with_traceback(tb)
E pytest_assume.plugin.FailedAssumption:
E 2 Failed Assumptions:
E
E test-assume.py:5: AssumptionFailure
E >> pytest.assume(x == y)
E AssertionError: assert False
E
E test-assume.py:7: AssumptionFailure
E >> pytest.assume(False)
E AssertionError: assert False
/usr/local/lib/python3.7/site-packages/six.py:692: FailedAssumption
================================================================= 3 failed in 0.31s ==================================================================
Most helpful comment
Sorry, I've tried to reproduce it with simple example with no success despite the fact I've found a way to make my tests work again.
I've fixed the pb (for me) by deleting a
__init__.pyin the first sub directory of my tests directory. I'm not sure it's the way to go, I'm relatively new in Python programming.