DEV Community

portfield
portfield

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)

Collapse
 
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 conftest.py 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
Collapse
 
tterb profile image
Brett Stevenson

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

Collapse
 
portfield10 profile image
portfield

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.

Collapse
 
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.

Collapse
 
portfield10 profile image
portfield

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.