Package: unittest-autocomp
Bash completion for Pythons unittestRunning 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 thetestsdirectory.
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_nameIf I made a mistake during copy & paste I get an unhelpful error.As long as runningpython -m unittest discoveris sufficient this isn't a problem. But there are situations when this is not desired:the whole test suite takes a long time to completesome test cases do not run on my machine locallythe 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 thetest_*.pypattern relative to the current working directory. It parses each file into anabstract syntax tree ASTto 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.Callingunittest-autocompwithout any arguments will print all test case files relative to the current working dir:$unittest-autocomp
test.test_completionstest.test_utilsCallingunittest-autocompwith a full path to a test case file will print all classes of this file:$unittest-autocomptest.test_completions.

test.test_completions.MethodnameCompletionTestCasetest.test_completions.ClassnameCompletionTestCasetest.test_completions.ModuleCompletionTestCase(The trailing dot is required, so that the program knows to search the file)Callingunittest-autocompwith a full path to a unittest class will print all test methods of that given class:$unittest-autocomptest.test_completions.MethodnameCompletionTestCase.


test.test_completions.MethodnameCompletionTestCase.test_completion_with_empty_partialtest.test_completions.MethodnameCompletionTestCase.test_completion_with_only_folder_or_moduletest.test_completions.MethodnameCompletionTestCase.test_completion_with_missing_classnametest.test_completions.MethodnameCompletionTestCase.test_completion_with_only_classnametest.test_completions.MethodnameCompletionTestCase.test_completion_with_only_classname_and_half_method_nametest.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?Make surebash-completionis installed:sudo apt install bash-completionInstall this module:pip install unittest-autocompActivate 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.dBy default this installation will yield completions for the commandpython -m unittest. If you invoke the unittest module otherwise you need to change the lineCMD="python -m unittest"locally on your machine to match your invocation.