0

I'm getting the following error when try to use pytest to run my tests:

tests/main_test.py:1: in <module>
    from src.main import main
src/main.py:1: in <module>
    from string_helper import string_sum
E   ModuleNotFoundError: No module named 'string_helper'

My project is very simple, here is the structure:

enter image description here

src/main.py

from string_helper import string_sum

def main():
  a = 2
  b = 1
  r = string_sum(a, b)
  #print(r)
  return r

if __name__ == "__main__":
  r = main()
  print(r)

src/string_helper.py

def string_sum(a, b):
  return str(a + b)

tests/main_test.py

from src.main import main

def test_string_sum():
  r = main(1, 2)
  assert type(r) is str
  assert r == "3"

Here is the repl.it if you want to run it in web browser: https://replit.com/@gmunumel/TestCasesExample?v=1. Run pytest -v and install the latest version of the package. You should see the error.

Gabriel Muñumel
  • 1,876
  • 6
  • 34
  • 57
  • One way - you could put the code inside a module (/src/mymodule/main.py), and use absolute imports (`mymodule.main`,`mymodule.string_helper`). When running tests, add `src` to `sys.path` and import from `mymodule`. – Peter Jun 09 '22 at 16:30
  • @Peter, thanks but didn't work for me. I found an easiest solution here: [https://stackoverflow.com/a/50610630/992347](https://stackoverflow.com/a/50610630/992347) – Gabriel Muñumel Jun 10 '22 at 16:26

0 Answers0