Tutorial: Visual testing with pytest

Posted on by Roberto Pesce

If you are like many Python developers out there you LOVE writing code! Tests? Not so much. Whether it’s the concept of testing or the interface of testing tools, testing is a chasm not all developers cross. The Visual Testing with pytest tutorial aims to bring Python testing to the masses, taking a new approach to first contact with Python testing.

Join this journey with us and improve your testing skills in this 9-step tutorial!

The tutorial

As mentioned before, this tutorial was split into 9 steps. The idea is to make each step unique so you can watch and apply your learning as you go. Each tutorial step has a narrated video along with a detailed writeup, connected to working code.

Here’s an overview and below you can find a description for each step.

  • Step 1 – Background on Testing: Introduce testing, pytest, and how it all fits into PyCharm, along with a scenario to work on.
  • Step 2 – Project Setup: Make a PyCharm project and virtual environment with dependencies, then configure PyCharm to use pytest.
  • Step 3 – Hello Test: Write a simple test and run it in PyCharm’s visual test runner.
  • Step 4 – Getting Into the TDD Flow: Setup your PyCharm project to help get into the flow of Test-Driven Development (TDD).
  • Step 5 – Jump to Error: Writing code means writing broken code. Click a link in a traceback to open a file on the line of the error.
  • Step 6 – Skipping Tests: During refactoring, use pytest’s markers to ignore certain breaking tests.
  • Step 7 – Test Fixtures: Make your tests more focused by moving sample data to pytest fixtures.
  • Step 8 – Debugging During Testing: When writing tests, use the PyCharm “visual debugger” to interactively poke around in the context of a problem.
  • Step 9 – Testing Exceptions: Write tests that ensure exceptions are raised when expected.

About pytest

The pytest framework makes it easy to write small tests, yet scales to support complex functional testing for applications and libraries. For more information about the pytest framework read their official documentation.

About the PyCharm Guide

Looking to level up your Python and PyCharm skills? The PyCharm Guide takes a visual approach, with tips, tutorials, and more.

Comments below can no longer be edited.

10 Responses to Tutorial: Visual testing with pytest

  1. Anders Hovmöller says:

    June 16, 2020

    My best tip for using pytest with pycharm is to install pytest-pycharm. https://github.com/jlubcke/pytest-pycharm

    It’s the difference between “well that sucked” and “this is exactly what I want from a debugger”.

    • Paul Everitt says:

      June 16, 2020

      Funny enough, that functionality will be in tomorrow’s EAP. No kidding. Here’s the ticket: https://youtrack.jetbrains.com/issue/PY-9848

      If I have time, I will give a demo of it at the end of my Python Web Conference tutorial tomorrow morning.

  2. Cappy says:

    June 17, 2020

    @Paul Everitt
    Great tutorial. I’ve been looking for a walkthrough like this.

    In Step 2, you refer to doing pip install -e .[tests]
    but don’t state what this is nor why a user *should* do this. What happens if they don’t do this?

    I think this “editable install” may be a bit more advanced for learning how to use Pytest from Pycharm though.

    “Python Testing with Pytest” also refers to this editable install, but doesn’t really state what or why.

    • Paul Everitt says:

      June 17, 2020

      It follows what pytest documents as best practices. It’s explained on their page, explained on my PyCharm Guide page that goes with that video (“Most Python packages put their…”), and mentioned when I show the pytest “strongly suggested” paragraph.

      I considered showing the quick way, but decided against it. Probably a controversial decision, but decided to bite the bullet and show the right way.

  3. Cappy says:

    June 17, 2020

    In Step 3 @ the 14-second mark, you click on Player, then suddenly a popup window to create a test shows up, but you don’t state how to get to that window/menu.

    It would help if you slowed down too. I have to go back to the video and rewind it to see how you get from one place to the next.

    • Paul Everitt says:

      June 17, 2020

      I viewed the videos as companions to the written doc. Those that want the full explanation have a resource, those that want a concise walkthrough have the video. But there probably are a few places where the jumping happens too fast.

      In the example you give, though, I explain “Let PyCharm create our test for us by using Navigate | Go To Test” and the presentation assistant window pops up, showing the key I pressed.

  4. TC says:

    June 20, 2020

    In Step 7 at 1:32 where you say “Wave the magic wand”. It looks like Pycharm is automatically converting the remaining functions to use the new guardians fixture.

    What is the key sequence that makes that happen? The video just zips through that, there is no way to see the keystrokes in the green popup bar that shows the keys that were pressed.

    • Paul Everitt says:

      June 22, 2020

      Sorry, “magic wand” was insufficiently clear. I simple meant: “I’m going to type all of this, and I’m going to use video software to make it go really fast, because the specific details aren’t necessary to the video.”

  5. xtian says:

    June 26, 2020

    Can I perform this tutorial with the Free version?

    • Roberto Pesce says:

      June 26, 2020

      Hi, yes, you can use PyCharm Community to perform this tutorial.

Subscribe

Subscribe for updates