Skip to content

Migrate tests to pytest and cleanup

Migrate all swh-web unit tests from unittest to pure pytest which implies the following:

  • all tests are turned from a class method to a simple function

  • replace previous assertions from unittest.TestCase to simple calls to Python assert

  • previous features offered by the former TestCase classes are turned into pytest fixtures

  • use pytest-mock fixture instead of decorator from unittest.patch

  • use requests_mock_datadir fixture from swh-core simplifying the setup of mocked HTTP requests

  • expose some Django assertions to be used with pytest

  • remove the swh.web.tests.testcase module

Some cleanups have also been performed on the tests implementation, notably:

  • Better formatting of string litterals

  • Removal of tests related to origin ids

WARNING: This diff is huge and most of its content is not really interesting to review as it mainly consists of turning test methods to simple functions and replace self.assert* calls by simple calls to Python assert.

I pushed it to validate the pytest fixtures use. The following ones are used in the tests:

  • client: Django test client fixture offered by pytest-django

  • api_client: Fixture to get the Django REST framework test client (defined in conftest.py)

  • test_archive: Fixture to manipulate the data from the sample archive used in the tests (defined in conftest.py). Previously, the features it offers were defined as methods in the former WebTestCase class.

  • mocker: Fixture wrapping the patching API provided by the unittest.mock module offered by pytest-mock

Kudos to the great unittest2pytest package as its use was a real time saver here when migrating the tests.

After that migration, the tests running time is decreased by a factor of 5-6x (around 35s when using unittest, around 7s when using pytest).

Related to #2062 (closed)

Test Plan

tox


Migrated from D2242 (view on Phabricator)

Merge request reports