Почему VS Code не может напрямую запускать Python unittest .py или обнаруживать его на вкладке «Тестирование»Python

Программы на Python
Anonymous
Почему VS Code не может напрямую запускать Python unittest .py или обнаруживать его на вкладке «Тестирование»

Сообщение Anonymous »


I was having issues with this when learning Python unittest for a larger project, so I reproduced it for a simple boilerplate project. I make a folder structure like this and have the project open in VS Code from the top directory.

unittestproject ├──examplepackage ├────__init__.py ├──test ├────__init__.py I make a virtual environment by pressing Ctrl+Shift+P and using Python: Create Environment from the command palette to make a .venv Virtual environment for the project. I don't think that's relevant to the issue but I'll mention it.

I make a py file in /examplepackage called add.py that just contains a single function that adds two arguments and returns the result.

I make a py file in /test called testadd.py with the following contents...

import unittest from examplepackage import add class TestAdd(unittest.TestCase): def test_add(self): self.assertEqual(add.add(1,2),3) if __name__ == "__main__": unittest.main() I can run this test successfully in the following ways from the terminal, while located in the unittestproject directory.

python -m unittest python -m unittest test/testadd.py python -m unittest test.testadd This is good, but if I try to directly run testadd.py using the play button in the upper right corner, I get a ModuleNotFoundError...

$ f:/Python/unittestproject/.venv/Scripts/python.exe f:/Python/unittestproject/test/testadd.py Traceback (most recent call last): File "f:\Python\unittestproject\test\testadd.py", line 2, in from examplepackage import add ModuleNotFoundError: No module named 'examplepackage' Also, whenever I try to set up tests in the "Testing" tab, which creates the following settings.json file...

{ "python.testing.unittestArgs": [ "-v", "-s", "./test", "-p", "test*.py" ], "python.testing.pytestEnabled": false, "python.testing.unittestEnabled": true } I keep seeing an error that confusingly relates to PyTest even though I specified that I want to use unittest, NOT PyTest.

[error] f:\Python\unittestproject\.venv\Scripts\python.exe: No module named pytest [error] Subprocess exited unsuccessfully with exit code 1 and signal null on workspace f:\Python\unittestproject. [error] Subprocess exited unsuccessfully with exit code 1 and signal null on workspace f:\Python\unittestproject. Creating and sending error discovery payload [error] pytest test discovery error for workspace: f:\Python\unittestproject And so my questions are:
  • Why does running the script through the unittest module handle the import fine, but when running the script directly, Python is unable to see the adjacent package?
  • Why is Python IntelliSense in VS Code having no trouble resolving that package despite this?
  • Why is the VS Code "Testing" tab insisting on using PyTest, and why is it not discovering the tests that match the regex?

I've tried to resolve these on my own and with other forum questions over the last few hours, but what others are saying should work is not working for me. One example, appending "project." to the front of the package import does not resolve either. I have also tried adding __init__.py to the top directory.

EDIT: I don't know why this is, maybe it's a bug or something. But I closed VS Code and came back to it later, and tests loaded up in the Testing tab no problem. It was using unittest instead of PyTest and running them worked fine. This might be a bug.


Источник: https://stackoverflow.com/questions/780 ... esting-tab

Вернуться в «Python»