Skip to main content

Bash completion for Pythons unittest

Running single test cases with the unittest module is a bit fiddly. At least for me it often looks a bit like this:

I know that the test resides somewhere in the tests directory. Therefore, I need to find the correct file: tests/test_foo.py. But damn it! There are multiple test case classes in it. But I only want to run one of them. So I type: python -m unittest tests.test_foo.SomeFancyTestCase. Now I notice that this test case has multiple methods and each method takes ages to complete. But I only want to run this one test case that is repeatedly failing. So I need to open the file in order to get the full method name. Nice, the command now looks like this: python -m unittest tests.test_foo.SomeFancyTestCase.test_a_specific_edge_case_with_a_long_name If I made a mistake during copy & paste I get an unhelpful error.

As long as running python -m unittest discover is sufficient this isn't a problem. But there are situations when this is not desired:

  • the whole test suite takes a long time to complete
  • some test cases do not run on my machine locally
  • the application that is being tested is overly chatty and I only want logs regarding this one test case
  • ...

Therefore, I created this little helper script to add Bash autocompletion.

How does it work?

The program looks recursively for test case files matching the test_*.py pattern relative to the current working directory. It parses each file into an abstract syntax tree AST to offer completions for class names and/or method names.

The unittest module expects the identifier of a test case in the following format:

<MODULE>.<TEST_CLASS>.<TEST_METHOD>

where <TEST_CLASS> and <TEST_METHOD> are additional specifications to limit the number of executed tests. But these are optional.

Calling unittest-autocomp without any arguments will print all test case files relative to the current working dir:

$ unittest-autocomp
test.test_completions test.test_utils

Calling unittest-autocomp with a full path to a test case file will print all classes of this file:

$ unittest-autocomp test.test_completions.

test.test_completions.MethodnameCompletionTestCase test.test_completions.ClassnameCompletionTestCase test.test_completions.ModuleCompletionTestCase

(The trailing dot is required, so that the program knows to search the file)

Calling unittest-autocomp with a full path to a unittest class will print all test methods of that given class:

$ unittest-autocomp test.test_completions.MethodnameCompletionTestCase.


test.test_completions.MethodnameCompletionTestCase.test_completion_with_empty_partial test.test_completions.MethodnameCompletionTestCase.test_completion_with_only_folder_or_module test.test_completions.MethodnameCompletionTestCase.test_completion_with_missing_classname test.test_completions.MethodnameCompletionTestCase.test_completion_with_only_classname test.test_completions.MethodnameCompletionTestCase.test_completion_with_only_classname_and_half_method_name test.test_completions.MethodnameCompletionTestCase.test_completion_with_only_full_method_name

(The trailing dot is required, so that the program knows to search the class for methods)

Installation?

  1. Make sure bash-completion is installed:
    • sudo apt install bash-completion
  2. Install this module:
    • pip install unittest-autocomp
  3. Activate the completion:
    • paste the file: ./unittest-completion to /etc/bash_completion.d:
      • sudo wget https://github.com/M0r13n/unittest-autocomp/blob/main/unittest-completion -P /etc/bash_completion.d

By default this installation will yield completions for the command python -m unittest. If you invoke the unittest module otherwise you need to change the line CMD="python -m unittest" locally on your machine to match your invocation.

Metadata

Release files for unittest-autocomp 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for unittest-autocomp 0.0.1
File Size Uploaded Provenance
unittest-autocomp-0.0.1.tar.gz 7.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for unittest-autocomp 0.0.1
File Interpreter ABI Platform
unittest_autocomp-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 13.1 kB

Release files / unittest-autocomp-0.0.1.tar.gz

Download URL unittest-autocomp-0.0.1.tar.gz
Size 7.7 kB
Tags Source
SHA-256 checksum
How to use checksums
a297fb994b5cf554fc0d2ac19aeb9683138837cf0651fe34e65653a0afdfd80b
BLAKE2b-256 checksum
How to use checksums
97707e4c3d90aef2b4ce41cafee387a7bf097fb6a65c4e6d5cf6c9bfa0b26c80
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.4

Release files / unittest_autocomp-0.0.1-py3-none-any.whl

Download URL unittest_autocomp-0.0.1-py3-none-any.whl
Size 5.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
049580a2ef80e3e605cecd5f6a65b4765e81a9239fd694a82b9aa22d8e497186
BLAKE2b-256 checksum
How to use checksums
cdc9a6d4ee1005a2cf44ff399620ee69f9a7f344b45d9efc72351920014799fb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.4

Security for unittest-autocomp 0.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page