Skip to content
GitLab
Projects Groups Topics Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
  • Sign in
  • autosubmit autosubmit
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributor statistics
    • Graph
    • Compare revisions
  • Issues 338
    • Issues 338
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 21
    • Merge requests 21
  • Deployments
    • Deployments
    • Releases
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Commits
  • Issue Boards
Collapse sidebar
  • Earth SciencesEarth Sciences
  • autosubmitautosubmit
  • Issues
  • #961
Closed
Open
Issue created Feb 24, 2023 by Bruno de Paula Kinoshita@bdepaulaMaintainer

[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.

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):
    ...
Assignee
Assign to
Time tracking