I have the following structure:
- project/source_code/constants.py
- project/source_code/calculator.py
- project/tests.py
# In 'constants.py'
MIN_VALUE = 0
MAX_VALUE = 100
# In 'calculator.py'
import constants
class Calculator:
#Create initialiser (not used at the moment)
def __init__(self):
return
def get_sum(self, number1, number2):
return number1 + number2
def main():
calculator = Calculator()
print(calculator.get_sum(10, 10))
return
if __name__ == "__main__":
main()
print("")
exit(0)
# In 'tests.py'
import unittest
import source_code.calculator as Calculator
class Test_Calculator(unittest.TestCase):
def setUp(self):
self.calculator = Calculator()
return
def test_calculator(self, number1, number2, errorMessage):
self.assertEqual(self.get_sum(number1, number2), expected_LBTT, errorMessage)
return
def main(self):
test_calculator(10, 10, "The calculated sum is wrong.")
return
if __name__ == "__main__":
unittest.main()
When I run calculator.py, it works fine.
When I run either tests.py or python -m unittest, I get the following error:
> ======================================================================
> **ERROR: tests (unittest.loader._FailedTest.tests)
> **----------------------------------------------------------------------
> ImportError: Failed to import test module: tests
> Traceback (most recent call last):
> File "C:\Users\Home\AppData\Local\Programs\Python\Python311\Lib\unittest\loader.py", line 407, in _find_test_path
> module = self._get_module_from_name(name)
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> File "C:\Users\Home\AppData\Local\Programs\Python\Python311\Lib\unittest\loader.py", line 350, in _get_module_from_name
> __import__(name)
> File "C:\Users\Home\Desktop\BJSS\project\tests.py", line 2, in <module>
> import source_code.calculator as Calculator
> File "C:\Users\Home\Desktop\BJSS\project\source_code\calculator.py", line 1, in <module>
> import constants
> ModuleNotFoundError: No module named 'constants'
>
>
> ----------------------------------------------------------------------
> Ran 1 test in 0.000s
>
> FAILED (errors=1)
I don't understand why it runs one way, but not the other. How can I fix this?