Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How do I run one unittest test while debugging a function?

Quick answer

Use unittest’s dotted test name to run one method: python -m unittest test_pricing.TestPrice.test_discount. Replace the module, class, and method with the names in your project. You can also select tests by name pattern with -k. After investigating, run python -m unittest for discovery or python -m unittest discover to check the broader suite. These commands rely on importable test modules and a compatible project layout. [S1]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I’m debugging a function and don’t want to run every test after each small change. How do I run just the test for it? Say my file is test_pricing.py, with a class called TestPrice and a method called test_discount.

Geez

Give unittest the test’s fully qualified name: python -m unittest test_pricing.TestPrice.test_discount. For your example, that asks unittest to run test_discount from TestPrice in the importable test_pricing module. This is the most direct choice when you know the exact method. The documented command-line form supports selecting a module, class, or individual method. [S1]

While debugging, keep the test focused: change one part of the function, run that command again, and compare the result with what the test expects. For instance, if apply_discount is the function under investigation, the test can call it with a small, fixed input and check the expected result. The names in the command must match your actual test module, class, and method; the example names here are placeholders.

Follow-up question

What if I don’t remember the exact method name, but I know the relevant tests have “discount” in their names?

Geez

Use -k with a substring or pattern to select tests whose fully qualified names match it. For example: python -m unittest -k discount. This can select matching tests without spelling out one complete dotted method name. [S1]

A practical approach is to choose a distinctive term, rather than a very broad one that might match unrelated tests. If the selection doesn’t identify the test you intended, check the test names in your code and use the exact dotted form instead. The command selects by names; it does not establish that the chosen test covers every behavior of the function.

Follow-up question

Can you show a small test and explain how to use it during the investigation?

Geez

Here is an illustrative example; adapt the function’s inputs and expected result to your own requirements:

import unittest
from pricing import apply_discount

class TestPrice(unittest.TestCase):
    def test_discount(self):
        self.assertEqual(apply_discount(100, 10), 90)

Assuming the module can be imported from your project’s test-running location, run python -m unittest test_pricing.TestPrice.test_discount. The example is not a claim about how your function should behave: 100, 10, and 90 are placeholders for a case whose expected result you have decided.

If the test fails, use that small case to inspect the inputs, the function’s intermediate calculations, and the actual versus expected result. Make one change at a time and rerun the same command. A passing focused test tells you that this selected case passes; by itself, it does not show that other cases or other parts of the project are correct.

Follow-up question

Once I’ve fixed the issue, how do I switch back and avoid forgetting the wider check?

Geez

Run python -m unittest to use discovery, or explicitly run python -m unittest discover. These are the documented options for returning to a broader suite. [S1] The available commands depend on importable test modules and the project layout, so if discovery does not find your tests, check how your project organizes and imports them. [S1]

A reusable sequence is: run one named test; investigate with a small, understood case; rerun that test after a change; then run discovery before treating the investigation as finished. Keep the focused command handy, but don’t treat it as a replacement for the broader check.

A practical checklist

  • Copy the module, class, and method names from the test you want to run.

  • Run python -m unittest module.Class.method for one specific test.

  • Use python -m unittest -k pattern to select tests by a name pattern or substring.

  • Change one thing at a time and rerun the focused command.

  • After debugging, run python -m unittest or python -m unittest discover.

  • If a command cannot find tests, check that the modules are importable and fit the project layout.

A few more useful answers

Do I include `.py` in the dotted test name?

The documented selection form uses an importable test module name, such as test_module.TestClass.test_method, rather than a filename with .py. Adapt the example to the module name available in your project. [S1]

Does one passing test prove the function is fully correct?

No. It shows only that the selected case passes. Run the broader suite afterward, and add or run focused cases for other behaviors you need to check.

What if the exact command doesn’t find my test?

Check that the module name, class, and method are spelled as they appear in your tests, and that the test module is importable from where you run the command. The documented commands depend on importability and project layout. [S1]

Sources

References used for this guide.

  1. [S1] Python 3.12 unittest: Command-line test selection and discovery
    docs.python.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work