0

I'm having a problem running unit tests. I have a project structure like this:

Given this directory

who-said-what/
    |
    |_ wave_encoder.py
    | 
    |_ tests/
        |_ test_wave_encoder.py

where test_wave_encoder.py looks like this:

from wave_encoder import *

class TestEncoder():
    def test_plot_no_fit1(self):
        encoder = WaveEncoder()
        self.assertRaises(ValueError, encoder.plot_signal)

    def test_plot_no_fit2(self):
        encoder = WaveEncoder()
        self.assertRaises(ValueError, encoder.plot_transform)

    def test_plot_no_fit3(self):
        encoder = WaveEncoder()
        self.assertRaises(ValueError, encoder.plot_components)

If I run this test file individually, no issues. However, if I try to run pytest from any directory in the project:

pytest -v --cov ./tests 
# or
pytest -v --cov .

I get a ModuleNotFoundError: No module named 'wave_encoder'.

However, if I move test_wave_encoder.py to the parent directory, it does work (there are other errors, but that's a different question).

I don't really want a bunch of test files in the parent directory. How do I sort this out?

rocksNwaves
  • 5,331
  • 4
  • 38
  • 77

1 Answers1

0

Step 1: place an empty conftest.py in your root. Step 2: run tests from root folder with python -m pytest

That should work.

JarroVGIT
  • 4,291
  • 1
  • 17
  • 29
  • Okay, that did indeed work. Why is this method required as apposed to what I did, which came from a RealPython tutorial I just watched? Just trying to learn. And further, will this method be compatible with CircleCI (also something I'm learning on RealPython) – rocksNwaves Aug 05 '20 at 21:38
  • 1
    To be honest, I just ran into the same problem today and that is how I figured:p the first step tells purest what the root is (and thus, where tests and actual code is). This is used as pythonpath within pytest. The second step is a windows with virtual environment thing. I noticed that pytest would run against my global Python rather then my intended virtual env Python if I just called `pytest`. When I am in an activated env, pytest must be run in that env. Please accept the answer if it fixed your issue:) kudos are always welcome – JarroVGIT Aug 05 '20 at 21:41
  • 1
    I can't accept for another 3 minutes, but I will. – rocksNwaves Aug 05 '20 at 21:43