jeudi 7 décembre 2017

ModuleNotFound: No module named 'app'

Let's say I have the following folder structure intended to organize the tests in my project repo:

project/
    app.py
    tests/
        test.py

app.py has a simple addition function:

def add(x, y):
    return x + y

test.py tests this function:

from app import add
import unittest

class TestCase(unittest.TestCase):
    def test_add(self):
        print('Running the test!')
        self.assertEqual(add(3, 4), 7)

if __name__ == '__main__':
    unittest.main()

When I use nose2 everything works:

mac:project $ nose2
Running the test!
Ran 1 test in 0.000s
Ok

The problem is when I try to run test.py directly I get the following error:

mac:project $ python tests/test.py
ModuleNotFound: No module named 'app'

How can I fix this? The reason is I want to use the coverage package to measure code coverage and I'm getting the same error:

mac:project $ coverage run tests/test.py
ModuleNotFound: No module named 'app'

I've tried from ..app import app but I get this error: ValueError: attempted relative import beyond top-level package

Aucun commentaire:

Enregistrer un commentaire