rdmo icon indicating copy to clipboard operation
rdmo copied to clipboard

Test performance: Mock out `pypandoc.convert_text` when testing export views

Open afuetterer opened this issue 7 months ago • 0 comments

After having failed with my previous attempt to reduce testing time, I present this idea:

Let's mock out pypandoc.convert_text when testing export views.

Through pytest parametrization there are a couple of tests, that run 100+ times.

For example these tests run num_times = u * e (number of users * number of export formats).

@pytest.mark.parametrize('username,password', users)
@pytest.mark.parametrize('export_format', export_formats)
def test_export(db, client, username, password, export_format):

The export view tests actually run pypandoc.convert_text every time. But only the status code of the response is checked. The exported text file, e.g. pdf is never checked. Therefore I suggest mocking pypandoc.convert_text. This way each test can still test the response status code, but the real pypandoc does not need to convert the data. This will save time given the large number of tests doing this.

@pytest.fixture
def mocked_convert_text(mocker):
    """Mock the pypandoc.convert_text function.

    `mocked_convert_text` can be used in tests of the export views.
    Use it to assert pypandoc would have been called with:
    mocked_convert_text.assert_called(), mocked_convert_text.assert_called_once() or
    mocked_convert_text.assert_called_once_with().

    See:
    - <https://pytest-mock.readthedocs.io/en/latest/usage.html>
    - <https://docs.python.org/3/library/unittest.mock.html#unittest.mock.MagicMock>
    """
    from rdmo.core.utils import pypandoc  # noqa: F401
    return mocker.patch("pypandoc.convert_text")

I suggest checking the export view separately once, maybe using the request factory and to actually check the returned data. Which exported rdmo model would be best for this?

What do you think?

afuetterer avatar Jul 29 '24 19:07 afuetterer