8000 allow usage of unittest's load_tests protocol in python unit tests · Issue #12063 · microsoft/vscode-python · GitHub
[go: up one dir, main page]

Skip to content
allow usage of unittest's load_tests protocol in python unit tests #12063
Open
@benjaminbecker

Description

@benjaminbecker

I have a project with the following structure:

.
├── functions.py
├── __init__.py
└── tests
    ├── __init__.py
    ├── test_functions.py

This is the content of test_functions.py:

import unittest
import os.path as path

from functions import sum


current_dir = path.dirname(path.abspath(__file__))
test_file = path.join(current_dir, 'foo.txt')


class TestFunctions(unittest.TestCase):

    def test_sum(self):
        self.assertEqual(sum(1, 2), 3)

    def test_file_exists(self):
        self.assertTrue(path.exists(test_file))

In order to make the second test succeed I need to run some code before it. I do this by using unittest's load_test protocol. It is implemented in tests/__init__.py:

import os
import unittest
from .test_functions import TestFunctions, test_file


class CustomTestSuite(unittest.TestSuite
6679
):

    def run(self, result):
        with open(test_file, "w") as file:
            file.write("")
        super().run(result)
        os.remove(test_file)



def load_tests(loader, default_tests, pattern):
    # top level directory cached on loader instance
    test_suite = CustomTestSuite()
    tests = loader.loadTestsFromTestCase(TestFunctions)
    test_suite.addTests(tests)
    return test_suite

If I run python -m unittest from the root dir, everything works fine:

..
----------------------------------------------------------------------
Ran 2 tests in 0.001s

OK

I could not manage to use my custom test suite within the vs code python extension. If there is currently no solution, I would like to request this feature.

I am aware of other solutions like using setUp per test or module for this simple example. However in my real project the setup involves more complex steps like initializing an email server.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0