RSE-Sheffield / team-python

Notes and Issues for Team Python
4 stars 0 forks source link

Testing: Why use pytest instead of unittest? #13

Closed Joe-Heffer-Shef closed 5 months ago

Joe-Heffer-Shef commented 1 year ago

My question: what are the advantages of using pytest rather than the inbuilt unittest library?

Does it help implement testing best practices and methodologies?

Why would I install pytest when unittest is built in to the core libraries?

What can I do with pytest that I can't do with other libraries?

Pytest claims:

The pytest framework makes it easy to write small, readable tests, and can scale to support complex functional testing for applications and libraries.

unittest claims:

The unittest unit testing framework was originally inspired by JUnit and has a similar flavor as major unit testing frameworks in other languages. It supports test automation, sharing of setup and shutdown code for tests, aggregation of tests into collections, and independence of the tests from the reporting framework.

What do these concepts mean?

ns-rse commented 1 year ago

I use pytest but only because its the framework used when I learnt about writing tests. I've never used unittest to be able to give a comparison.

Features I like about pytest...

I'd be curious to find out more about unittest but having learnt and being familiar with pytest would be reluctant to switch (acquired knowledge and learning curve both being barriers to doing so).

My understanding is that a "test suite" is simply an existing set of tests for a given set of code.

Not sure about "test runner" the pytest-runner extension/plugin is deprecated but appears to be a package that runs pytest automatically on a package. No idea of its meaning in other frameworks.

ns-rse commented 9 months ago

Pytest Plugins for Flask testing...

I've never used Flask so can't comment on how useful they are for testing but still love using pytest for my test development and use various plugins (of which there are lots) such as pytest-regtest (although they've an issue concerning dependency on py which I suggested a solution for over a year ago but hasn't been accepted or closed), pytest-mpl and pytest-github-actions-annotate-failures.

ns-rse commented 6 months ago

Conditional coverage if you only want to calculate these statistics under certain conditions.

ns-rse commented 6 months ago

Just in time...

Pytest vs. Unittest: Which Is Better? | The PyCharm Blog

Joe-Heffer-Shef commented 6 months ago

Pytest Parameterisation by Neil https://blog.nshephard.dev/posts/pytest-param/

Joe-Heffer-Shef commented 6 months ago

Parallel test execution with pytest-xdist https://pytest-xdist.readthedocs.io/

ns-rse commented 6 months ago

Lots of useful Pytest extensions including...

ns-rse commented 6 months ago

Additional ways of testing.

tdjames1 commented 6 months ago

Fixtures and mocking

https://docs.python.org/3/library/unittest.html#class-and-module-fixtures

Parameterisation

Keeping tests small

Order of evaluation

Regression testing

Regression test plugin https://gitlab.com/uweschmitt/pytest-regtest

https://github.com/CITCOM-project/CausalTestingFramework

"Regression testing is a common technique to implement basic testing before refactoring legacy code that lacks a test suite."

Other testing frameworks are available

https://hypothesis.readthedocs.io/en/latest/ https://docs.python.org/3/library/doctest.html

twinkarma commented 5 months ago

Covered