[enhancement] Use pytest instead of unittest for writing tests for Autosubmit
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.
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):
...