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

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

Open
benjaminbecker opened this issue May 29, 2020 · 2 comments
Open
Assignees
Labels
area-testing feature-request Request for new features or functionality needs PR Ready to be worked on

Comments

@benjaminbecker
Copy link

benjaminbecker commented May 29, 2020

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.

@benjaminbecker benjaminbecker added triage-needed Needs assignment to the proper sub-team feature-request Request for new features or functionality labels May 29, 2020
@ghost ghost removed the triage-needed Needs assignment to the proper sub-team label Jun 2, 2020
@karthiknadig
Copy link
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.

@luabud luabud added the triage-needed Needs assignment to the proper sub-team label Jul 8, 2020
@karthiknadig karthiknadig self-assigned this Jul 9, 2020
@karthiknadig karthiknadig added area-testing triage and removed triage-needed Needs assignment to the proper sub-team needs decision labels Jul 9, 2020
@karthiknadig karthiknadig added investigating We are looking into the cause of the issue and removed triage labels Sep 15, 2020
@karthiknadig karthiknadig added needs PR and removed investigating We are looking into the cause of the issue labels Aug 6, 2021
@karthiknadig karthiknadig removed their assignment Aug 6, 2021
@github-actions github-actions bot removed the needs PR label Aug 9, 2022
@karrtikr karrtikr added the needs PR Ready to be worked on label Aug 9, 2022
@eleanorjboyd eleanorjboyd self-assigned this Dec 4, 2023
@eleanorjboyd
Copy link
Member

eleanorjboyd commented Dec 20, 2023

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
Labels
area-testing feature-request Request for new features or functionality needs PR Ready to be worked on
Projects
None yet
Development

No branches or pull requests

5 participants