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
  write code that resides in parent directory franklin97355 3 322 Apr-14-2024, 02:03 AM
Last Post: franklin97355
  problem in running a code akbarza 7 616 Feb-14-2024, 02:57 PM
Last Post: snippsat
  writing and running code in vscode without saving it akbarza 1 377 Jan-11-2024, 02:59 PM
Last Post: deanhystad
  the order of running code in a decorator function akbarza 2 515 Nov-10-2023, 08:09 AM
Last Post: akbarza
  package script cant find sibling script when executed from outside Bock 3 865 Mar-03-2023, 04:26 PM
Last Post: snippsat
  Running script with subprocess in another directory paul18fr 1 3,664 Jan-20-2023, 02:33 PM
Last Post: paul18fr
  Code running many times nad not just one? korenron 4 1,349 Jul-24-2022, 08:12 AM
Last Post: korenron
  Error while running code on VSC maiya 4 3,699 Jul-01-2022, 02:51 PM
Last Post: maiya
  code running for more than an hour now, yet didn't get any result, what should I do? aiden 2 1,485 Apr-06-2022, 03:41 PM
Last Post: Gribouillis
  Why is this Python code running twice? mcva 5 5,249 Feb-02-2022, 10:21 AM
Last Post: mcva

Forum Jump:

User Panel Messages

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