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

[enhancement] Use pytest instead of unittest for writing tests for Autosubmit #961

Open
kinow opened this issue Feb 24, 2023 · 1 comment · May be fixed by #1879
Open

[enhancement] Use pytest instead of unittest for writing tests for Autosubmit #961

kinow opened this issue Feb 24, 2023 · 1 comment · May be fixed by #1879
Assignees

Comments

@kinow
Copy link
Member

kinow commented Feb 24, 2023

I'm starting to look into writing tests for the RO-Crate and cat-log merge requests, but I really miss the fixtures in pytest, and also not having to write a class, then methods, etc.

For me one of the biggest wins of using pytest is adding fixtures that can be reused in several other tests. With unitest we can create objects needed for the tests in the setUp function, but then if we have another file with another test that needs something similar (or parts of it) then we have to design some OO way to share that to avoid duplication.For example, see these two different test files.

image

image

There is a lot of boilerplate code that is duplicated (I think there are other test files with similar code). With pytest we could have a conftest.py file at the root of the test directory, with something like:

import pytest
import random
from autosubmit.experiment.experiment_common import base36encode

@ pytest.fixture
def expid():
    """Autosubmit expid is a base-36 radix value, with at least
    4 characters.

    We first pick a random number between 0 and the last possible
    number to produce a 4-character expid ``(36 ^ 4) - 1``, so
    ``1_679_615`` (we do not need to test with len > 4)."""
    random_int = random.randint(0, 1_679_615)
    return base36encode(random_int)

@ pytest.fixture
def job_list(expid, mocker):
    as_conf = mocker.Mock()
    as_conf.experiment_data = dict()
    as_conf.experiment_data["JOBS"] = dict()
    as_conf.jobs_data = as_conf.experiment_data["JOBS"]
    as_conf.experiment_data["PLATFORMS"] = dict()
    return JobList(expid, FakeBasicConfig, YAMLParserFactory(...), JobListPersistenceDb(...), as_conf)  

And I could use it in any test with:

def test_job_graph(job_list):
    ...

def test_synchronize_date_group_date(job_list):
    ...
@kinow
Copy link
Member Author

kinow commented Mar 22, 2023

@kinow kinow self-assigned this Dec 12, 2024
@kinow kinow linked a pull request Dec 12, 2024 that will close this issue
28 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant