Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solving the confusion generated by too many ways to run unittest in python

I am trying to implement a full and clean way of testing python packages, one that would suit the folowing requirements:

  • execute tests on clean machines without setting them up (virtualenv)
  • gather results from multiple platforms
  • gather results from multiple python interpreters
  • code coverage (merge results from the multiple execution into a single report)
  • be able to generate xml code coverage report so the build system can track how this change over time.
  • be able to enable disable tests based on platform
  • run several commands before the tests, like pep8 or autopep8
  • run tests in parallel.

I used several approaches: nose + pytest, tox + pytest but recently discovered that pytest should be able to do most of the stuff.

The main problem is that I wasn't able to find a clear comparision regarding when it would be better to use one approach or another.

Can someone explain these and give some use cases or limitations of these configurations? ... just to make it clear when when you go for one approach or another.

In the end I do want to have these options:

  • quicktest - run the tests locally, a must before each commmit
  • fulltest - full tests, running them across all available platforms, a must before making a new release
like image 409
sorin Avatar asked Apr 17 '13 12:04

sorin


People also ask

Which is better Pytest or unittest?

Which is better – pytest or unittest? Although both the frameworks are great for performing testing in python, pytest is easier to work with. The code in pytest is simple, compact, and efficient. For unittest, we will have to import modules, create a class and define the testing functions within that class.

Which function in unittest will run all of your tests?

TestCase is used to create test cases by subclassing it. The last block of the code at the bottom allows us to run all the tests just by running the file.


1 Answers

py.test covers all your needs perfectly it's pros comparing to other test runners like nose:

  • fixtures with dependency injection - eliminate the need of complicated oop for test setup
  • simple but powerful plugin system with lots of useful plugins - plugins like pytest-xdist allow you to test on all platforms as you need, including windows, pytest-cov does the coverage, pytest-cache helps to run only last failed tests, etc
  • parametrization allows you to use write-once run-multiple approach for tests using declarative parameters
  • use of the simple assert statement vs complicated java-like syntax .assertEquals (http://pytest.org/latest/assert.html)
  • more and more
like image 153
Anatoly Bubenkov Avatar answered Sep 25 '22 12:09

Anatoly Bubenkov