- Nov 2020
-
github.com github.com
-
Comparison between pytest and unittes test frameworks
Detailed comparison table of pytest vs unittest modules (check below)
Tags
Annotators
URL
-
- Apr 2020
-
code.visualstudio.com code.visualstudio.com
-
For example, the test_decrement functions given earlier are failing because the assertion itself is faulty.
Debugging tests themselves
- Set a breakpoint on the first line of the failing function (e.g.
test_decrement
) - Click the "Debug Test" option above the function
- Open Debug Console and type:
inc_dec.decrement(3)
to see what is the actual output when we use x=3 - Stop the debugger and correct the tests
- Save the test file and run the tests again to look for a positive result
- Set a breakpoint on the first line of the failing function (e.g.
-
Once VS Code recognizes tests, it provides several ways to run those tests
After discovering tests, we can run them, for example, using CodeLens:
-
You can trigger test discovery at any time using the Python: Discover Tests command.
After using
python.testing.autoTestDiscoverOnSaveEnabled
, it'll be set totrue
and discovering tests whenever a test file is saved.If discovery succeeds, the status bar shows Run Tests instead:
-
Testing in Python is disabled by default. To enable testing, use the Python: Configure Tests command on the Command Palette.
Start testing in VS Code by using
Python: Configure Tests
(it automatically chooses one testing framework and disables the rest).Otherwise, you can configure tests manually by setting only one of the following to True:
python.testing.unittestEnabled
python.testing.pytestEnabled
python.testing.nosetestsEnabled
-
python.testing.unittestArgs: Looks for any Python (.py) file with "test" in the name in the top-level project folder.
Default behaviour of test discovery by unittest framework
-
Create a file named test_unittest.py that contains a test class with two test methods
Sample test file using unittest framework.
inc_dec
is the file that's being tested:import inc_dec # The code to test import unittest # The test framework class Test_TestIncrementDecrement(unittest.TestCase): def test_increment(self): self.assertEqual(inc_dec.increment(3), 4) # checks if the results is 4 when x = 3 def test_decrement(self): self.assertEqual(inc_dec.decrement(3), 4) if __name__ == '__main__': unittest.main()
-
Each test framework has its own conventions for naming test files and structuring the tests within, as described in the following sections. Each case includes two test methods, one of which is intentionally set to fail for the purposes of demonstration.
- each testing framework has own naming conventions
- each case includes two test methods (one of which fails)
-
Nose2, the successor to Nose, is just unittest with plugins
Nose2 testing
-
Python tests are Python classes that reside in separate files from the code being tested.
-
general background on unit testing, see Unit Testing on Wikipedia. For a variety of useful unit test examples, see https://github.com/gwtw/py-sorting
-
Running the unit test early and often means that you quickly catch regressions, which are unexpected changes in the behavior of code that previously passed all its unit tests.
Regressions
-
Developers typically run unit tests even before committing code to a repository; gated check-in systems can also run unit tests before merging a commit.
When to run unit tests:
- before committing
- ideally before merging
- many CI systems run it after every build
-
in unit testing you avoid external dependencies and use mock data or otherwise simulated inputs
Unit tests are small, isolated piece of code making them quick and inexpensive to run
-
The practice of test-driven development is where you actually write the tests first, then write the code to pass more and more tests until all of them pass.
Essence of TDD
-
The combined results of all the tests is your test report
test report
-
each test is very simple: invoke the function with an argument and assert the expected return value.
e.g. test of an exact number entry:
def test_validator_valid_string(): # The exact assertion call depends on the framework as well assert(validate_account_number_format("1234567890"), true)
-
Unit tests are concerned only with the unit's interface—its arguments and return values—not with its implementation
-
unit is a specific piece of code to be tested, such as a function or a class. Unit tests are then other pieces of code that specifically exercise the code unit with a full range of different inputs, including boundary and edge cases.
Essence of unit testing
-