0

I want to test using pytest. As I have read, it is good practice to have the following structure:

project/
  home.py
  tests/
    test_home.py

In my file test_home.py I have:

import pytest

How can I import home.py to test it?

Nathaniel Jones
  • 939
  • 1
  • 14
  • 25
Arturo Aviles
  • 179
  • 4
  • 16
  • 2
    The trick is to run your tests from the `project` folder. Then you can just use `import home`. – Klaus D. Feb 07 '18 at 02:32
  • I tried your trick, went to my `project` directory and ran "pytest" in the bash. It found the test_home.py but it returned that it couldn't find home. Could it be that I'm inside a virtualenv? – Arturo Aviles Feb 07 '18 at 16:16
  • Oh I found out how, I will write the answer below, your trick works but I needed to have a __init__.py in my test directory. Thanks @KlausD. – Arturo Aviles Feb 07 '18 at 16:23

1 Answers1

0

It order to run pytest with a project structure as mentioned above, you need to have a __init__.py file inside the tests directory. Also make sure to run pytest from the project directory.

Arturo Aviles
  • 179
  • 4
  • 16