Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stestr ignores PYTHONPATH? #278

Open
vykozlov opened this issue Oct 17, 2019 · 3 comments
Open

stestr ignores PYTHONPATH? #278

vykozlov opened this issue Oct 17, 2019 · 3 comments
Labels

Comments

@vykozlov
Copy link

vykozlov commented Oct 17, 2019

Issue description
Test ('test_unit.py') does not run because of the ImportError of another module. Direct execution of the test_unit.py works, as well as running via pytest.

Expected behavior and actual behavior
Expected that the test runs fine.
Actual behavior: fails to run

Steps to reproduce the problem
I have the following structure:

/top_dir
    |-- my_package
        |-- src
            |--  __init__.py
            |-- model
                |-- __init__.py
                |-- app.py
            |-- tests
                |-- __init__.py
                |-- test_unit.py
    |-- common_tools
        |-- tool_one
            |-- __init__.py
            |-- tool_one_config.py
        |--- tools.py

PYTHONPATH=/top_dir/common_tools

.stestr.conf is located in /top_dir/my_package:

[DEFAULT]
test_path=./src/tests/
top_dir=./src

app.py has import tools as tl
tools.py has from tool_one import tool_one_config
running "python app.py" works fine.
running "pytest src/tests/test_unit.py" is also fine.
However, running from /top_dir/my_package "stestr -v run" fails with

from tool_one import tool_one_config
ImportError: cannot import name 'tool_one_config'

Specifications like the version of the project, operating system, or hardware
System information
Ubuntu 16.04
python 3.5.2
stestr 2.5.1

Additional information

@vykozlov vykozlov added the bug label Oct 17, 2019
@mtreinish
Copy link
Owner

mtreinish commented Dec 5, 2019

I tried to recreate this locally but I'm unable to get it to fail. Granted I may not have perfectly mirrored the structure you have in your project but I created locally this structure:

$ ls -R test_package

test_package:
common_tools/
my_package/

test_package/common_tools:
tool_one/
tools.py


test_package/common_tools/tool_one:
__init__.py
tool_one_config.py


test_package/my_package:
src/

test_package/my_package/src:
__init__.py
model/
tests/

test_package/my_package/src/model:
app.py
__init__.py


test_package/my_package/src/tests:
__init__.py
test_unit.py

While I didn't have any contents in the modules since I don't know what you're doing for these I did put import statements as you outlined:

$ cat test_package/common_tools/tools.py
from tool_one import tool_one_config
$ cat test_package/my_package/src/model/app.py
import tools as tl

I did write a single test case in test_unit.py and imported app just to verify it doesn't error from that:

$ cat test_package/my_package/src/tests/test_unit.py
import unittest

from model import app

class TestFoo(unittest.TestCase):

    def test_fun(self):
        self.assertTrue(True)

Then running stestr with PYTHONPATH set to the absolute path to common_tools

$ PYTHONPATH=/tmp/test_package/common_tools stestr run
{0} tests.test_unit.TestFoo.test_fun [0.000106s] ... ok

======
Totals
======
Ran: 1 tests in 0.0001 sec.
 - Passed: 1
 - Skipped: 0
 - Expected Fail: 0
 - Unexpected Success: 0
 - Failed: 0
Sum of execute time for each test: 0.0001 sec.

==============
Worker Balance
==============
 - Worker 0 (1 tests) => 0:00:00.000106

This is actually consistent with my expectations, stestr launches each test worker (and does test discovery) in a subprocess that doesn't explicitly set env, which means all the env variables from the parent process get passed through to the workers. So when we launch new processes to do the actual execution here: https://github.com/mtreinish/stestr/blob/master/stestr/test_processor.py#L188 (which gets called here https://github.com/mtreinish/stestr/blob/master/stestr/test_processor.py#L202 and https://github.com/mtreinish/stestr/blob/master/stestr/test_processor.py#L241) the PYTHONPATH env variable should be set when it launches the new python binary to actually run the tests.

I'm not going to close this yet since I might have missed something in my reproduction attempt, but I'm not able to debug any potential issue here without being able to reproduce the failure. Is the specific project that's failing for you open source or hosted somewhere I can access and try this locally?

@vykozlov
Copy link
Author

vykozlov commented Dec 5, 2019

Hi @mtreinish ,

Thank you very much for testing this. My configuration can actually work if I change .stestr.conf as:

[DEFAULT]
test_path=./src/tests/
top_dir=./

i.e instead of top_dir=./src specify top_dir=./ . I still do not fully get why this works, as the PYTHONPATH is set as PYTHONPATH=/top_dir/common_tools in both cases...

@mtreinish
Copy link
Owner

Interesting, it did work with me with the stestr.conf as:

[DEFAULT]
test_path=./src/tests/
top_dir=./src

located in my_package (per the issue description)

I was going to suggest you change the top dir when I first read the issue because top_dir is basically the cwd for discovery. It's what unittest treats as the root of the project. But I didn't suggest that because it looked like your PYTHONPATH variable was an absolute path and it shouldn't make a difference if your manually adding the absolute path of common_tools python's search path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants