Skip to content

allow usage of unittest's load_tests protocol in python unit tests #12063

Open

Description

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):

    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.

Activity

  1. ghost removed
    triage-neededNeeds assignment to the proper sub-team
    on Jun 2, 2020
  2. karthiknadig commented on Jun 2, 2020

    @karthiknadig
    Member

    Thank you for the suggestion! We have marked this issue as "needs decision" to make sure we have a conversation about your idea. We plan to leave this feature request open for at least a month to see how many 👍 votes the opening comment gets to help us make our decision.

  3. eleanorjboyd commented on Dec 20, 2023

    @eleanorjboyd

    As an update here, I am going to have to rewrite a bit of how unittest works to allow for django complatibility. When I do so, I will switch to calling unittest more generally (instead of recreating the steps unittest takes). With this change this issue should be resolved as well (or at least make sure this use case is also taken care of during that other engineering work). #73

    edit, this issue better describes this: #22388

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions