0

Below test works as expected when I execute from PyCharm but when I attempt to run python TestData.py from command line I receive :

Traceback (most recent call last):
  File "TestData.py", line 3, in <module>
    from service.DataAccessor import Data
ModuleNotFoundError: No module named 'service'

When I try python -m unittest

I receive:

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

Both commands are run from project directory.

Project structure is:

project/service
 - DataAccessor.py
project/test
 - TestData.py

Code:

TestData.py :

    import unittest

    from service.DataAccessor import Data

    class TestData(unittest.TestCase):

        def test_get_data(self):
            print(Data.getDf(self))
            self.assertEqual(3, 3)

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

DataAccessor.py:

import pandas as pd

class Data():

    def getDf(self):

        df = pd.read_csv("./data.csv")
        return df
blue-sky
  • 51,962
  • 152
  • 427
  • 752
  • 1
    Looks like `service` is not in the Python path. Either change the import to include `project` (I guess that is in the path), or add `service` to the path while running the tests. Or you run the test from inside `project`, see for example [this question](https://stackoverflow.com/questions/1896918/running-unittest-with-typical-test-directory-structure). – MrBean Bremen Apr 12 '20 at 15:52

1 Answers1

0

Coming from Java background I'm not 100% sure why this works but adding an empty __init__.py to the dirs: test & service and executed python -m unittest test.TestData executes the test.

blue-sky
  • 51,962
  • 152
  • 427
  • 752