Python Forum
Running tests in a sibling directory to code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Running tests in a sibling directory to code
#1
In a shared root, I have a src and a tests folder. When from that root I run pipenv run pytest ., I get the error that my auth module can't be found for this test:

from auth.models.user import User

class TestUser:
    def test_valid_pass():
        assert User.validate_pass("abcdefgh")
Someone suggested I create a setup.py, which I did, but when I do, I have to turn my import into from src.auth.models.user import User, but then all my modules require being redone to src.whatever. I can't figure out how to properly path my tests.

This is my setup.py https://gist.github.com/rohitsodhia/7496...d2cc90bc49

Updated my setup.py to set

packges=find_packages(where="src/*"),

It seems to now pick up all the files directly under src, but not any of the directories, like src/auth
Reply
#2
use source as top level package: don't use package_dir, look for packages in the project root dir (packages=find_packages() without explicitly stating where

More info on packages remapping can be found in Listing whole packages section.

One thing to remember! add __init__.py to every subfolder you are importing from... you may also want to look at
sys.path.insert(0, os.getcwd())
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need consultation about running tests for a Github repo Askic 0 329 Apr-27-2025, 01:30 PM
Last Post: Askic
  code not running even without errors Azdaghost 2 545 Apr-25-2025, 07:35 PM
Last Post: Azdaghost
  python code not running Azdaghost 1 420 Apr-22-2025, 08:44 PM
Last Post: deanhystad
  writing and running code in vscode without saving it akbarza 5 2,915 Mar-03-2025, 08:14 PM
Last Post: Gribouillis
  write code that resides in parent directory franklin97355 3 1,592 Apr-14-2024, 02:03 AM
Last Post: franklin97355
  problem in running a code akbarza 7 2,610 Feb-14-2024, 02:57 PM
Last Post: snippsat
  the order of running code in a decorator function akbarza 2 1,504 Nov-10-2023, 08:09 AM
Last Post: akbarza
  package script cant find sibling script when executed from outside Bock 3 1,953 Mar-03-2023, 04:26 PM
Last Post: snippsat
  Running script with subprocess in another directory paul18fr 1 13,401 Jan-20-2023, 02:33 PM
Last Post: paul18fr
  Code running many times nad not just one? korenron 4 2,372 Jul-24-2022, 08:12 AM
Last Post: korenron

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020