Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unittest tests will not run with unittest.main() in the test file #17342

Closed
Tracked by #17242
TheCatSaber opened this issue Sep 9, 2021 · 4 comments
Closed
Tracked by #17242

Unittest tests will not run with unittest.main() in the test file #17342

TheCatSaber opened this issue Sep 9, 2021 · 4 comments
Assignees
Labels
area-testing bug Issue identified by VS Code Team member as probable bug

Comments

@TheCatSaber
Copy link

Environment data

  • VS Code version: 1.60.0 (user setup) (Commit: e7d7e9a9348e6a8cc8c03f877d39cb72e5dfb1ff)
  • Extension version (available under the Extensions sidebar): v2021.9.1191016588
  • OS and version: Windows_NT x64 10.0.19042
  • Python version (& distribution if applicable, e.g. Anaconda): 3.9.7
  • Type of virtual environment used N/A
  • Value of the python.languageServer setting: Pylance

Expected behaviour

When you run the tests from the Testing side bar in the extension, tests run properly when you have unittest.main() in a file.

Actual behaviour

Unittest Discovery Error [Folder Name]
Error discovering unittest tests (see Output > Python):

(with nothing in Output > Python).

Steps to reproduce:

[NOTE: Self-contained, minimal reproducing code samples are extremely helpful and will expedite addressing your issue]

  1. Create a new folder, and create add.py and test.py
  2. In add.py put:
def add(a, b):
    return a + b
  1. In tests.py put:
import unittest
from add import add

class TestAdd(unitest.TestCase):
    def test_one_plus_one(self):
        self.assertEqual(2, add(1, 1))
  1. Using Testing on the sidebar select Set-up python tests > unittest > . > test*.py
  2. Run the tests, tests work normally ✅
  3. Add unitest.main() to the bottom of test.py.
  4. Run test.py with python test.py: tests work normally ✅
  5. Try running tests with the Testing side bar: tests do not work, and you get the error described above ❌

Logs

Output for Python in the Output panel (ViewOutput, change the drop-down the upper-right of the Output panel to Python)

None

@TheCatSaber TheCatSaber added bug Issue identified by VS Code Team member as probable bug triage-needed Needs assignment to the proper sub-team labels Sep 9, 2021
@karthiknadig karthiknadig self-assigned this Sep 9, 2021
@karthiknadig
Copy link
Member

If you use unittest.main() then that can try to execute tests when the unittest loader loads this. So this should work better if you protect it like this. This is the recommended way of protecting code from running on import.

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

@karthiknadig karthiknadig added area-testing triage and removed triage-needed Needs assignment to the proper sub-team labels Sep 9, 2021
@TheCatSaber
Copy link
Author

  1. I should have been doing if __name__ == '__main__': anyway.
  2. That fixed the problem.

I think it would be a good idea to make the error more explicit (i.e. to try to detect the running of the tests, and output a message to the user, saying to use if __name__ == '__main__').

@brettcannon
Copy link
Member

We are working on rewriting our detection/execution code which should simply ignore the initial run as a side-effect of importing.

@karthiknadig
Copy link
Member

Closing in favor of #17242

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 27, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-testing bug Issue identified by VS Code Team member as probable bug
Projects
None yet
Development

No branches or pull requests

3 participants