Skip to content

unitest (Standard Unit testing) Framework

Don Jayamanne edited this page Oct 30, 2016 · 7 revisions

This section outlines the details necessary to get you up and started with using the python unittest testing framework with Visual Studio Code.

Enable unittest framework

Assign the value true against the setting python.unitTest.unittestEnabled as outlined here.
Ensure all other test frameworks have been disabled (i.e. have the value false).

Configuration Options

Test Discovery Pattern

The default pattern used to match test files is test*.py. This can be configured as follows:

  • Open the user or workspace settings (settings.json)
  • Add the configuration item ("-p", "*_test.py") if not found (else alter it as follows):
    "python.unitTest.unittestArgs": [
        "-p", 
        "*_test.py"
    ],

Note:

  • The above is merely an example that picks all files ending with _test.p to be treated as test files.
  • Further details and values of this config item can be found here.

Verbosity

The default verbosity is 'v'. This can be configured as follows:

  • Open the user or workspace settings (settings.json)
  • Add the configuration item ("-v") if not found (else alter it as follows):
    "python.unitTest.unittestArgs": [
        "-v"
    ],
  • If you do not require a verbose output, simple remove the "-v" entry as follows:
    "python.unitTest.unittestArgs": [
    ],

Start Directory

This is the directory to start the test discovery (defaults to the project/workspace root directory).
This can be configured as follows:

  • Open the user or workspace settings (settings.json)
  • Add the configuration item ("-s", "./tests") if not found (else alter it as follows):
    "python.unitTest.unittestArgs": [
        "-v",
        "-s",
        "./tests"
    ],

Note:

  • Further details and values of this config item can be found here.

Fail Fast

Use this option to stop the test run on the first error or failure.
By default this is disabled, i.e. if a test fails the test run continues with other tests. This can be configured as follows:

  • Open the user or workspace settings (settings.json)
  • Add the configuration item ("-f") if not found (else alter it as follows):
    "python.unitTest.unittestArgs": [
        "-v",
        "-f"
    ],

Note:

  • Further details and values of this config item can be found here.

Test Discovery

Both the Start Directory and Test Discovery Pattern play a role in the discovery of unit tests.
Sometimes unit tests placed in sub-directories will not get discovered. This is mainly because of the fact that such test files are not importable. For instance, the tests in the db sub-directory will not get discovered, untill and unelsss an __init__.py file is placed in that directory.

Hence, the solution is simple:

  • Place a __init__.py file in each directory containing the unittests if they are to be discovered.
├── LICENSE
├── models
│   └── __init__.py
├── README.md
├── requirements.txt
├── tc.py
├── tests
│   ├── db
│   │   ├── __init__.py       # NEW
│   │   └── test_employee.py
│   ├── __init__.py           # NEW
│   └── test_tc.py
└── todo.txt
Clone this wiki locally