Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
afuetterer opened this issue Jul 29, 2024 · 0 comments
Assignees

Comments

@afuetterer
Copy link
Member

afuetterer commented Jul 29, 2024

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant