mercredi 6 juillet 2016

Python3 test import error

I am using python3 to try and get a test file for sample application working yet it keeps throwing ImportError: No module named 'calculate'

my file structure is:

/calculate
  __init__.py
  calculate.py
  test/
    __init__.py
    calculate_test.py

I cannot figure out why this is the case, any help would be much appreciated.

The __init__.py files are empty.

calculate.py contains:

class Calculate(object):
    def add(self, x, y):
        return x + y

if __name__ == '__main__':
    calc = Calculate()
    result = calc.add(2, 2)
    print(result)

calculate_test.py contains:

import unittest
from calculate import Calculate

class TestCalculate(unittest.TestCase):
    def setUp(self):
        self.calc = Calculate()

    def test_add_method_returns_correct_result(self):
        self.assertEqual(4, self.calc.add(2,2))

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

I am running python test/calculate_test.py from the root /calculate folder and am getting the error

Traceback (most recent call last):
  File "test/calculate_test.py", line 2, in <module>
    from calculate import Calculate
ImportError: No module named 'calculate'

I have been fiddling around with different structures and cannot understand what the problem is.

Aucun commentaire:

Enregistrer un commentaire