Friday, 3 December 2021

How to set up imported modules to work in both debug and test

I am trying to set up a workspace in VSC and I want to be able to run it in venv.

root
- app
-/- modules
-/-/- __init__.py
-/-/- myModule.py
-/-/- myModule2.py
-/- __init__.py
-/- myApp.py
- test
-/- myTest.py
- venv

myApp.py

import random
import numpy as np

import modules.myModule as mm

x = mm.main()

myModule.py

from app.modules import myModule2

def main():
    x = myModule2()
    return x

The issue I'm having is that my own modules can either be run in debug or in test (as unit test) but the same set up can not run for both. When I have from app.modules import myModule2 I can run test but not debug and when I have from modules import myModule2 I can run debug but not test.

my launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        }
    ]
}

my setting.json

{
    "python.testing.unittestArgs": [
        "-v",
        "-s",
        "./test",
        "-p",
        "*test.py"
    ],
    "python.testing.pytestEnabled": false,
    "python.testing.unittestEnabled": true,
    "python.analysis.extraPaths": [
        "./app"
    ]
}

How do I set up my workspace to be able to run both?

Edit:

import unittest
from app.modules.myModule import main

class Test_Foo(unittest.TestCase):
    def test_bar(self):
        x = main()
        assertEqual(x, 'foobar', 'not equal')


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

Added the test.

I run debug on the file myApp.py, and the test I run from test explorer.

EDIT2:

The error I get when running debug is

Exception has occurred: ModuleNotFoundError
No module named 'app'

The exception is caught in the file called myModule.py in line from app.modules import myModule2



from How to set up imported modules to work in both debug and test

No comments:

Post a Comment