forked from zed-industries/zed
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
python: Add runnable unittest tasks (zed-industries#12451)
Add runnable tasks for Python, starting with `unittest` from the standard library. Both `TestCase`s (classes meant to be a unit of testing) and individual test functions in a `TestCase` will have runnable icons. For completeness, I also included a task that will run `unittest` on the current file. The implementation follows the `unittest` CLI. The unittest module can be used from the command line to run tests from modules, classes or even individual test methods: ``` python -m unittest test_module.TestClass python -m unittest test_module.TestClass.test_method ``` ```python import unittest class TestStringMethods(unittest.TestCase): def test_upper(self): self.assertEqual('foo'.upper(), 'FOO') def test_isupper(self): self.assertTrue('FOO'.isupper()) self.assertFalse('Foo'.isupper()) def test_split(self): s = 'hello world' self.assertEqual(s.split(), ['hello', 'world']) # check that s.split fails when the separator is not a string with self.assertRaises(TypeError): s.split(2) if __name__ == '__main__': unittest.main() ``` From the snippet provided by `unittest` docs, a user may want to run test_split independently of the other test functions in the test case. Hence, I decided to make each test function runnable despite `TestCase`s being the unit of testing. ## Example of running a `TestCase` <img width="600" alt="image" src="https://github.com/zed-industries/zed/assets/16619392/7be38b71-9d51-4b44-9840-f819502d600a"> ## Example of running a test function in a `TestCase` <img width="600" alt="image" src="https://github.com/zed-industries/zed/assets/16619392/f0b6274c-4fa7-424e-a0f5-1dc723842046"> `unittest` will also run the `setUp` and `tearDown` fixtures. Eventually, I want to add the more commonly used `pytest` runnables (perhaps as an extension instead). Release Notes: - Added runnable tasks for Python `unittest`. ([zed-industries#12080](zed-industries#12080)).
- Loading branch information
Showing
3 changed files
with
122 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
; subclasses of unittest.TestCase or TestCase | ||
( | ||
(class_definition | ||
name: (identifier) @run @_unittest_class_name | ||
superclasses: (argument_list | ||
[(identifier) @_superclass | ||
(attribute (identifier) @_superclass)] | ||
) | ||
(#eq? @_superclass "TestCase") | ||
) @python-unittest-class | ||
(#set! tag python-unittest-class) | ||
) | ||
|
||
; test methods whose names start with `test` in a TestCase | ||
( | ||
(class_definition | ||
name: (identifier) @_unittest_class_name | ||
superclasses: (argument_list | ||
[(identifier) @_superclass | ||
(attribute (identifier) @_superclass)] | ||
) | ||
(#eq? @_superclass "TestCase") | ||
body: (block | ||
(function_definition | ||
name: (identifier) @run @_unittest_method_name | ||
(#match? @_unittest_method_name "^test.*") | ||
) @python-unittest-method | ||
(#set! tag python-unittest-method) | ||
) | ||
) | ||
) |