Hey, check out my Modern Python Projects course. It's an extended version of this workshop!

Testing

VS Code has a nice interface for running your tests. By default, it’s not visible, although the Python extension might sometimes ask you if you want to configure it. If it doesn’t, here is how to enable it:

Configure tests

  1. Open the command palette and run “Python: Configure tests”.
  2. From the list of available frameworks, select “pytest”.
  3. Finally, select the directory where you keep your tests (in my case, I’m using the root directory).

VS Code will blink a couple of times, and a magic potion icon should appear in the sidebar.

Configure tests

Here are the new superpowers that VS Code just enabled:

  • The magic potion icon is a “Test” tab. It contains a list of all the tests that VS Code found (across all the test files). You can also see at a glance which tests are passing and which are failing. At the top of the sidebar, you will find a few useful buttons:

    • Run all tests
    • Run failed tests
    • Debug all tests
    • Discover tests (use it if you added more tests, and they do not appear on the list)
    • Show test output
    • Collapse all
  • Above each test function, you will see small annotations: “Run Test” and “Debug Test” together with “✓” sign for passing tests and “⚠” sign for failing ones. You can put breakpoints in your tests and select the “Debug Test” option to start a debugger and see what’s going on in your test.

If you create a pytest.ini file and modify some pytest settings there, VS Code will respect those changes when running tests.