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

Move python tests from unittest to pytest with fixtures #756

Open
eivindjahren opened this issue Nov 24, 2020 · 0 comments
Open

Move python tests from unittest to pytest with fixtures #756

eivindjahren opened this issue Nov 24, 2020 · 0 comments

Comments

@eivindjahren
Copy link
Collaborator

eivindjahren commented Nov 24, 2020

Currently the test setup uses unittest, uses the EclTest class to inject information about where test-data is located and what testdata is available, and TestAreaContext to create temporary working directories. It would greatly simplify managing test data if this were to be moved to using pytest fixtures. As a bonus: this can be done on a per-test basis, and you don't need to move every file to pytest at once!

The GridCoarseTest uses all of these features:

https://github.com/equinor/ecl/blob/6bd69ef5c0a2455433cd67a7c622d62fab32bb74/python/tests/ecl_tests/test_grid_equinor_coarse.py#L24-L42

Here the TestAreaContext sets up the temporary directory to work in, @equinor_test marks this as a test that uses equinor data and createTestPath ammends the path to the test data directory. It would probably be cleaner to separate these conserns using pytest fixtures so that the finding of test data is abstracted away:

@pytest.fixture
def lgc_grid(test_data_path):
    return EclGrid(path.join(test_data_path, "Equinor/ECLIPSE/LGCcase/LGC_TESTCASE2.EGRID"))

Lets first look at the simple case of the assert statement at the end of the GridCoarseTest:

@pytest.mark.uses_equinor_data
def test_lgc_grid_num_coarse_groups(lgc_grid):
    assert lgc_grid.coarse_groups() == 3384

The pytest.mark.uses_equinor_data could be set up to skip the test unless a flag is given to pytest, or
the Equinor symlink is present as it is now.

As for the first part:

@pytest.mark.uses_equinor_data
def test_grid_save_roundtrip(lgc_grid, tmp_path):
    save_path = path.join(tmp_path, "LGC.EGRID")
    lgc_grid.save_EGRID(save_path)
    g2 = EclGrid(save_path)
    assert g1.equal(g2, verbose=True)
            
    lgc_grid.save_GRID(save_path)
    g3 = EclGrid(save_path)
    assert g1.equal(g3, verbose=True)

This uses the builtin tmp_path fixture to set up a temporary directory.

Fixtures can be collected into one module so that they will only have to be imported in conftest.py in order to be registered and available to all tests.

Really, test_grid_save_roundtrip should work with any grid, and we can create a fixture that reruns the test with
any number of grids:

@pytest.fixture(params=[
    "local/ECLIPSE/faarikaal/faarikaal1.EGRID",
    "Equinor/ECLIPSE/LGCCase/LGC_TESTCASE2.EGRID"
])
def any_grid(test_data_path, request):
  return EclGrid(path.join(test_data_path, request.param))

parametrized fixtures is very useful for cleaning up tests that now loop over a generated list of grids: https://github.com/equinor/ecl/blob/6bd69ef5c0a2455433cd67a7c622d62fab32bb74/python/tests/ecl_tests/test_grid.py#L410-L414

which could be written as

def test_volume(any_small_grid):
    cell_volumes = [grid.cell_volume(i) for i in range(grid.getGlobalSize())]
    assert sum(cell_volumes) == np.approx(single_cell_grid(grid).cell_volume(0)))

where any_small_grid is the parametrized fixture now generated by createVolumeTestGridBase.

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

No branches or pull requests

1 participant