DEV Community


Posted on

What components do you use to do TDD with Python?

I plan to use pytest, but I would like to know the pros and cons.

Top comments (5)

rhymes profile image
rhymes • Edited

I prefer pytest over unittest. The pros are:

  • you can write tests as functions (you can also use methods in classes like in unittest)
  • the mechanism allows you to group setups that are evaluated only at necessity in a cascading fashion in sub folders
  • the test function explicitly declares what mocks it needs to run
  • fixtures make a great way to organize tests that need a complex setup and tests that can run super fast and have initializers that can be run per session, per test, per file/module
  • there are no custom assertion methods (you use mostly assert and with)
  • it's unittest compatible
  • it has lots of plugins

the cons are:

  • it requires a little bit of time to understand how fixtures work
  • it's one more package to install
tterb profile image
Brett Stevenson

I agree with everything here, except I actually found pytest fixtures to be pretty intuitive.

portfield10 profile image

Thank you for your very valuable opinion!
This is exactly what I was looking for.

The disadvantages seemed to be fine for me,
I would like to verify using pytest.

rebeccaskinner profile image
Rebecca Skinner

I don’t practice TDD, but for testing python I’ve been using unittest along with MyPy. MyPy is a huge help in writing good working code and I can’t recommend it enough. It’s very very very far from perfect but it’s also light years ahead of the frustrating buggy mess of trying to write completely untyped code.

portfield10 profile image

Thank you for your valuable feedback!

I am a python beginner.
I didn't know Mypy, so it was helpful. Thank you again.

I am trying to verify TDD with reference to the unittest code created by my colleague.
If there are advantages to using pytest, I will change to pytest.