From 096217a0a3cb710e869d7e82a3d3609c4d968b76 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=C3=A9cile=20Daversin-Catty?= <cecile@simula.no>
Date: Tue, 20 Nov 2018 11:19:52 +0100
Subject: [PATCH] Pytest fixes - update unit tests to fit with last pytest
 version

---
 python/dolfin_utils/test/fixtures.py               |   5 +-
 python/test/unit/fem/test_dofmap.py                |  16 ++--
 python/test/unit/fem/test_finite_element.py        |   2 +-
 .../unit/fem/test_symbolic_geometry_assembly.py    | 101 ++++++++++++---------
 python/test/unit/function/test_function_space.py   |   4 +-
 python/test/unit/la/test_lu_solver.py              |   2 +-
 python/test/unit/la/test_nullspace.py              |   2 +-
 python/test/unit/mesh/test_mesh.py                 |  14 +--
 .../unit/multimesh/test_multimesh_cell_types.py    |  18 ++--
 python/test/unit/multimesh/test_multimesh_solve.py |  29 +++---
 10 files changed, 109 insertions(+), 84 deletions(-)

diff --git a/python/dolfin_utils/test/fixtures.py b/python/dolfin_utils/test/fixtures.py
index 790aa2f..a39b8aa 100644
--- a/python/dolfin_utils/test/fixtures.py
+++ b/python/dolfin_utils/test/fixtures.py
@@ -82,8 +82,6 @@ def gc_barrier():
     if MPI.size(MPI.comm_world) > 1:
         MPI.barrier(MPI.comm_world)
 
-
-@pytest.fixture
 def worker_id(request):
     """Returns thread id when running with pytest-xdist in parallel."""
     if hasattr(request.config, 'slaveinput'):
@@ -91,6 +89,9 @@ def worker_id(request):
     else:
         return 'master'
 
+@pytest.fixture
+def worker_id_fixture(request):
+    return worker_id(request)
 
 @pytest.yield_fixture(scope="function")
 def gc_barrier_fixture():
diff --git a/python/test/unit/fem/test_dofmap.py b/python/test/unit/fem/test_dofmap.py
index 2689ae5..7bf649d 100644
--- a/python/test/unit/fem/test_dofmap.py
+++ b/python/test/unit/fem/test_dofmap.py
@@ -41,8 +41,8 @@ reorder_dofs = set_parameters_fixture("reorder_dofs_serial", [True, False])
                                           (UnitCubeMesh, (2, 2, 2)),
                                           # cell.contains(Point) does not work correctly
                                           # for quad/hex cells once it is fixed, this test will pass
-                                          xfail((UnitSquareMesh.create, (4, 4, CellType.Type.quadrilateral))),
-                                          xfail((UnitCubeMesh.create, (2, 2, 2, CellType.Type.hexahedron)))])
+                                          pytest.param(((UnitSquareMesh.create, (4, 4, CellType.Type.quadrilateral))), marks=xfail),
+                                          pytest.param(((UnitCubeMesh.create, (2, 2, 2, CellType.Type.hexahedron))), marks=xfail)])
 def test_tabulate_all_coordinates(mesh_factory):
     func, args = mesh_factory
     mesh = func(*args)
@@ -516,16 +516,16 @@ xfail_ffc = pytest.mark.xfail(raises=Exception, strict=True)
     "FunctionSpace(UnitCubeMesh.create(2, 2, 2, CellType.Type.hexahedron),             'DQ', 2)",
     "FunctionSpace(UnitSquareMesh.create(6, 6, CellType.Type.triangle),                'N1curl', 1)",
     "FunctionSpace(UnitCubeMesh.create(2, 2, 2, CellType.Type.tetrahedron),            'N1curl', 1)",
-    xfail_ffc("FunctionSpace(UnitSquareMesh.create(6, 6, CellType.Type.quadrilateral), 'N1curl', 1)"),
-    xfail_ffc("FunctionSpace(UnitCubeMesh.create(2, 2, 2, CellType.Type.hexahedron),   'N1curl', 1)"),
+    pytest.param(("FunctionSpace(UnitSquareMesh.create(6, 6, CellType.Type.quadrilateral), 'N1curl', 1)"), marks=xfail_ffc),
+    pytest.param(("FunctionSpace(UnitCubeMesh.create(2, 2, 2, CellType.Type.hexahedron),   'N1curl', 1)"), marks=xfail_ffc),
     "FunctionSpace(UnitSquareMesh.create(6, 6, CellType.Type.triangle),                'N1curl', 2)",
     "FunctionSpace(UnitCubeMesh.create(2, 2, 2, CellType.Type.tetrahedron),            'N1curl', 2)",
-    xfail_ffc("FunctionSpace(UnitSquareMesh.create(6, 6, CellType.Type.quadrilateral), 'N1curl', 2)"),
-    xfail_ffc("FunctionSpace(UnitCubeMesh.create(2, 2, 2, CellType.Type.hexahedron),   'N1curl', 2)"),
+    pytest.param(("FunctionSpace(UnitSquareMesh.create(6, 6, CellType.Type.quadrilateral), 'N1curl', 2)"), marks=xfail_ffc),
+    pytest.param(("FunctionSpace(UnitCubeMesh.create(2, 2, 2, CellType.Type.hexahedron),   'N1curl', 2)"), marks=xfail_ffc),
     "FunctionSpace(UnitSquareMesh.create(6, 6, CellType.Type.triangle),                'RT', 1)",
     "FunctionSpace(UnitCubeMesh.create(2, 2, 2, CellType.Type.tetrahedron),            'RT', 1)",
-    xfail_ffc("FunctionSpace(UnitSquareMesh.create(6, 6, CellType.Type.quadrilateral), 'RT', 1)"),
-    xfail_ffc("FunctionSpace(UnitCubeMesh.create(2, 2, 2, CellType.Type.hexahedron),   'RT', 1)"),
+    pytest.param(("FunctionSpace(UnitSquareMesh.create(6, 6, CellType.Type.quadrilateral), 'RT', 1)"), marks=xfail_ffc),
+    pytest.param(("FunctionSpace(UnitCubeMesh.create(2, 2, 2, CellType.Type.hexahedron),   'RT', 1)"), marks=xfail_ffc),
 ])
 def test_dofs_dim(space):
     """Test function GenericDofMap::dofs(mesh, dim)"""
diff --git a/python/test/unit/fem/test_finite_element.py b/python/test/unit/fem/test_finite_element.py
index 0693f26..dee876c 100644
--- a/python/test/unit/fem/test_finite_element.py
+++ b/python/test/unit/fem/test_finite_element.py
@@ -34,7 +34,7 @@ xfail = pytest.mark.xfail(strict=True)
                                           (UnitSquareMesh.create, (4, 4, CellType.Type.quadrilateral)),
                                           # cell_normal has not been implemented for hex cell
                                           # cell.orientation() does not work
-                                          xfail((UnitCubeMesh.create, (2, 2, 2, CellType.Type.hexahedron)))])
+                                          pytest.param(((UnitCubeMesh.create, (2, 2, 2, CellType.Type.hexahedron))), marks=xfail)])
 def test_evaluate_dofs(mesh_factory):
 
     func, args = mesh_factory
diff --git a/python/test/unit/fem/test_symbolic_geometry_assembly.py b/python/test/unit/fem/test_symbolic_geometry_assembly.py
index f0597f9..3f89f7b 100644
--- a/python/test/unit/fem/test_symbolic_geometry_assembly.py
+++ b/python/test/unit/fem/test_symbolic_geometry_assembly.py
@@ -107,26 +107,20 @@ intervals.
 
 line_resolution = 8
 
-
-@fixture
-def line1d(request):
+def line1d_impl(request):
     n = line_resolution
     us = [i/float(n-1) for i in range(n)]
     vertices = [(u**2,) for u in us]
     return create_line_mesh(vertices)
 
-
-@fixture
-def rline1d(request):
+def rline1d_impl(request):
     n = line_resolution
     us = [i/float(n-1) for i in range(n)]
     vertices = [(u**2,) for u in us]
     vertices = list(reversed(vertices))  # same as line1d, just reversed here
     return create_line_mesh(vertices)
 
-
-@fixture
-def line2d(request):
+def line2d_impl(request):
     n = line_resolution
     us = [i/float(n-1) for i in range(n)]
     vertices = [(cos(DOLFIN_PI*u), sin(DOLFIN_PI*u)) for u in us]
@@ -134,9 +128,7 @@ def line2d(request):
     mesh.init_cell_orientations(Expression(("0.0", "1.0"), degree=0))
     return mesh
 
-
-@fixture
-def rline2d(request):
+def rline2d_impl(request):
     n = line_resolution
     us = [i/float(n-1) for i in range(n)]
     vertices = [(cos(DOLFIN_PI*u), sin(DOLFIN_PI*u)) for u in us]
@@ -145,9 +137,7 @@ def rline2d(request):
     mesh.init_cell_orientations(Expression(("0.0", "1.0"), degree=0))
     return mesh
 
-
-@fixture
-def line3d(request):
+def line3d_impl(request):
     n = line_resolution
     us = [i/float(n-1) for i in range(n)]
     vertices = [(cos(4.0*DOLFIN_PI*u),
@@ -156,9 +146,7 @@ def line3d(request):
     mesh = create_line_mesh(vertices)
     return mesh
 
-
-@fixture
-def rline3d(request):
+def rline3d_impl(request):
     n = line_resolution
     us = [i/float(n-1) for i in range(n)]
     vertices = [(cos(4.0*DOLFIN_PI*u),
@@ -168,9 +156,7 @@ def rline3d(request):
     mesh = create_line_mesh(vertices)
     return mesh
 
-
-@fixture
-def square2d(request):
+def square2d_impl(request):
     cellname = "triangle"
     side = sqrt(sqrt(3.0))
     vertices = [
@@ -186,9 +172,7 @@ def square2d(request):
     mesh = create_mesh(vertices, cells)
     return mesh
 
-
-@fixture
-def square3d(request):
+def square3d_impl(request):
     cellname = "triangle"
     vertices = [
         (0.0, 0.0, 1.0),
@@ -205,6 +189,37 @@ def square3d(request):
 
     return mesh
 
+@fixture
+def line1d(request):
+    return line1d_impl(request)
+
+@fixture
+def rline1d(request):
+    return rline1d_impl(request)
+
+@fixture
+def line2d(request):
+    return line2d_impl(request)
+
+@fixture
+def rline2d(request):
+    return rline2d_impl(request)
+
+@fixture
+def line3d(request):
+    return line3d_impl(request)
+
+@fixture
+def rline3d(request):
+    return rline3d_impl(request)
+
+@fixture
+def square2d(request):
+    return square2d_impl(request)
+
+@fixture
+def square3d(request):
+    return square3d_impl(request)
 
 @skip_in_parallel
 def test_line_meshes(line1d, line2d, line3d, rline1d, rline2d, rline3d):
@@ -238,14 +253,14 @@ def test_write_line_meshes_to_files(line1d, line2d, line3d, rline1d, rline2d,
                                                                   "DG", 0))
 
 
-@skip_in_parallel
 @pytest.mark.parametrize("mesh", [
-    line1d(None),
-    line2d(None),
-    line3d(None),
-    rline1d(None),
-    rline2d(None),
-    rline3d(None), ])
+    line1d_impl(None),
+    line2d_impl(None),
+    line3d_impl(None),
+    rline1d_impl(None),
+    rline2d_impl(None),
+    rline3d_impl(None), ])
+@skip_in_parallel
 def test_manifold_line_geometry(mesh, uflacs_representation_only):
     assert uflacs_representation_only == "uflacs"
     assert parameters["form_compiler"]["representation"] == "uflacs"
@@ -907,21 +922,21 @@ xfail_jit = pytest.mark.xfail(raises=Exception, strict=True)
     (UnitCubeMesh, (2, 2, 2)),
     (UnitSquareMesh.create, (4, 4, CellType.Type.quadrilateral)),
     (UnitCubeMesh.create, (2, 2, 2, CellType.Type.hexahedron)),
-    (line1d, (None,)),
-    (line2d, (None,)),
-    (line3d, (None,)),
-    (rline1d, (None,)),
-    (rline2d, (None,)),
-    (rline3d, (None,)),
-    (square2d, (None,)),
-    (square3d, (None,)),
+    (line1d_impl, (None,)),
+    (line2d_impl, (None,)),
+    (line3d_impl, (None,)),
+    (rline1d_impl, (None,)),
+    (rline2d_impl, (None,)),
+    (rline3d_impl, (None,)),
+    (square2d_impl, (None,)),
+    (square3d_impl, (None,)),
     # Tested geometric quantities are not implemented for higher-order cells
-    xfail_jit((UnitDiscMesh.create, (MPI.comm_world, 4, 2, 2))),
-    xfail_jit((UnitDiscMesh.create, (MPI.comm_world, 4, 2, 3))),
-    xfail_jit((SphericalShellMesh.create, (MPI.comm_world, 2,))),
-])
+    pytest.param(((UnitDiscMesh.create, (MPI.comm_world, 4, 2, 2))), marks=xfail_jit),
+    pytest.param(((UnitDiscMesh.create, (MPI.comm_world, 4, 2, 3))), marks=xfail_jit),
+    pytest.param(((SphericalShellMesh.create, (MPI.comm_world, 2,))), marks=xfail_jit)])
 @skip_in_parallel
 def test_geometric_quantities(uflacs_representation_only, mesh_factory):
+
     func, args = mesh_factory
     mesh = func(*args)
 
diff --git a/python/test/unit/function/test_function_space.py b/python/test/unit/function/test_function_space.py
index 3c97227..66cfe0e 100755
--- a/python/test/unit/function/test_function_space.py
+++ b/python/test/unit/function/test_function_space.py
@@ -165,13 +165,13 @@ def test_sub_equality(W, Q):
     assert Q.sub(0) == Q.extract_sub_space([0])
 
 
-def test_in_operator(f, g, V, V2, W, W2):
+def test_in_operator(f, g, V, V2, W, W2, mesh):
     assert f in V
     assert f in V2
     assert g in W
     assert g in W2
     with pytest.raises(RuntimeError):
-        mesh() in V
+        mesh in V
 
 
 def test_collapse(W, V):
diff --git a/python/test/unit/la/test_lu_solver.py b/python/test/unit/la/test_lu_solver.py
index aed2762..82790e9 100755
--- a/python/test/unit/la/test_lu_solver.py
+++ b/python/test/unit/la/test_lu_solver.py
@@ -21,7 +21,7 @@ from dolfin import *
 import pytest
 from dolfin_utils.test import skip_if_not_PETSc, skip_in_parallel
 
-backends = ["PETSc", skip_in_parallel("Eigen")]
+backends = ["PETSc", pytest.param(("Eigen"), marks=skip_in_parallel)]
 
 @pytest.mark.parametrize('backend', backends)
 def test_lu_solver(backend):
diff --git a/python/test/unit/la/test_nullspace.py b/python/test/unit/la/test_nullspace.py
index e60680a..a406947 100755
--- a/python/test/unit/la/test_nullspace.py
+++ b/python/test/unit/la/test_nullspace.py
@@ -21,7 +21,7 @@ from dolfin import *
 import pytest
 from dolfin_utils.test import *
 
-backends = ["PETSc", skip_in_parallel("Eigen")]
+backends = ["PETSc", pytest.param(("Eigen"), marks=skip_in_parallel)]
 
 def build_elastic_nullspace(V, x):
     """Function to build nullspace for 2D/3D elasticity"""
diff --git a/python/test/unit/mesh/test_mesh.py b/python/test/unit/mesh/test_mesh.py
index af7a3ad..d33673c 100644
--- a/python/test/unit/mesh/test_mesh.py
+++ b/python/test/unit/mesh/test_mesh.py
@@ -557,16 +557,16 @@ mesh_factories_broken_shared_entities = [
     (UnitIntervalMesh, (8,)),
     (UnitSquareMesh, (4, 4)),
     # FIXME: Problem in test_shared_entities
-    xfail_in_parallel((UnitDiscMesh.create, (MPI.comm_world, 10, 1, 2))),
-    xfail_in_parallel((UnitDiscMesh.create, (MPI.comm_world, 10, 2, 2))),
-    xfail_in_parallel((UnitDiscMesh.create, (MPI.comm_world, 10, 1, 3))),
-    xfail_in_parallel((UnitDiscMesh.create, (MPI.comm_world, 10, 2, 3))),
-    xfail_in_parallel((SphericalShellMesh.create, (MPI.comm_world, 1,))),
-    xfail_in_parallel((SphericalShellMesh.create, (MPI.comm_world, 2,))),
+    pytest.param(((UnitDiscMesh.create, (MPI.comm_world, 10, 1, 2))), marks=xfail_in_parallel),
+    pytest.param(((UnitDiscMesh.create, (MPI.comm_world, 10, 2, 2))), marks=xfail_in_parallel),
+    pytest.param(((UnitDiscMesh.create, (MPI.comm_world, 10, 1, 3))), marks=xfail_in_parallel),
+    pytest.param(((UnitDiscMesh.create, (MPI.comm_world, 10, 2, 3))), marks=xfail_in_parallel),
+    pytest.param(((SphericalShellMesh.create, (MPI.comm_world, 1,))), marks=xfail_in_parallel),
+    pytest.param(((SphericalShellMesh.create, (MPI.comm_world, 2,))), marks=xfail_in_parallel),
     (UnitCubeMesh, (2, 2, 2)),
     (UnitSquareMesh.create, (4, 4, CellType.Type.quadrilateral)),
     (UnitCubeMesh.create, (2, 2, 2, CellType.Type.hexahedron)),
-    xfail_in_parallel((create_two_good_quads, ())),
+    pytest.param(((create_two_good_quads, ())), marks=xfail_in_parallel),
 ]
 
 # FIXME: Fix this xfail
diff --git a/python/test/unit/multimesh/test_multimesh_cell_types.py b/python/test/unit/multimesh/test_multimesh_cell_types.py
index 76599f1..64ef136 100644
--- a/python/test/unit/multimesh/test_multimesh_cell_types.py
+++ b/python/test/unit/multimesh/test_multimesh_cell_types.py
@@ -25,8 +25,7 @@ import pytest
 from dolfin_utils.test import skip_in_parallel
 
 # test case with interface-edge overlap
-@pytest.fixture
-def test_case_1(M, N):
+def case_1_impl(M, N):
     multimesh = MultiMesh()
     mesh0 = UnitSquareMesh(M, M)
     mesh1 = RectangleMesh(Point(0.25, 0.25), Point(0.75, 0.75), N, N)
@@ -36,8 +35,7 @@ def test_case_1(M, N):
     return multimesh
 
 # test case with squares on the diagonal
-@pytest.fixture
-def test_case_2(width, offset, Nx):
+def case_2_impl(width, offset, Nx):
 
     # Mesh width (must be less than 1)
     assert width < 1
@@ -65,8 +63,16 @@ def test_case_2(width, offset, Nx):
     multimesh.build()
     return multimesh
 
-test_cases = [test_case_1(4,3),
-              test_case_2(DOLFIN_PI/5, 0.1111, 3)]
+@pytest.fixture
+def case_1(M, N):
+    return case_1_impl(M,N)
+
+@pytest.fixture
+def case_2(width, offset, Nx):
+    return case_2_impl(width, offset, Nx)
+
+test_cases = [case_1_impl(4,3),
+              case_2_impl(DOLFIN_PI/5, 0.1111, 3)]
 
 @skip_in_parallel
 @pytest.mark.parametrize("multimesh", test_cases)
diff --git a/python/test/unit/multimesh/test_multimesh_solve.py b/python/test/unit/multimesh/test_multimesh_solve.py
index 4530505..5edad0f 100644
--- a/python/test/unit/multimesh/test_multimesh_solve.py
+++ b/python/test/unit/multimesh/test_multimesh_solve.py
@@ -25,17 +25,13 @@ from dolfin import *
 
 from dolfin_utils.test import skip_in_parallel, fixture
 
-@fixture
-def exactsolution_2d():
+def exactsolution_2d_impl():
     return Expression("x[0] + x[1]", degree=1)
 
-@fixture
-def exactsolution_3d():
+def exactsolution_3d_impl():
     return Expression("x[0] + x[1] + x[2]", degree=1)
 
-@fixture
 def solve_multimesh_poisson(mesh_0, mesh_1, exactsolution):
-
     # Build multimesh
     multimesh = MultiMesh()
     multimesh.add(mesh_0)
@@ -81,9 +77,17 @@ def solve_multimesh_poisson(mesh_0, mesh_1, exactsolution):
 
     return uh
 
+@fixture
+def exactsolution_2d():
+    return exactsolution_2d_impl()
+
+@fixture
+def exactsolution_3d():
+    return exactsolution_3d_impl()
+
 @pytest.mark.slow
 @skip_in_parallel
-def test_multimesh_poisson_2d():
+def test_multimesh_poisson_2d(exactsolution_2d):
     # This tests solves a Poisson problem on two meshes in 2D with u =
     # x+y as exact solution
 
@@ -94,18 +98,17 @@ def test_multimesh_poisson_2d():
     mesh_1 = RectangleMesh(Point(0.1*DOLFIN_PI, 0.1*DOLFIN_PI),
                            Point(0.2*DOLFIN_PI, 0.2*DOLFIN_PI),
                            2, 2)
-
     # Solve multimesh Poisson
-    uh = solve_multimesh_poisson(mesh_0, mesh_1, exactsolution_2d())
+    uh = solve_multimesh_poisson(mesh_0, mesh_1, exactsolution_2d)
 
     # Check error
-    assert errornorm(exactsolution_2d(), uh, 'L2', degree_rise=1) < DOLFIN_EPS_LARGE
+    assert errornorm(exactsolution_2d, uh, 'L2', degree_rise=1) < DOLFIN_EPS_LARGE
 
 @pytest.mark.slow
 @pytest.mark.skip
 @skip_in_parallel
 @pytest.mark.skipif(True, reason="3D not fully implemented")
-def test_multimesh_poisson_3d():
+def test_multimesh_poisson_3d(exactsolution_3d):
     # This tests solves a Poisson problem on two meshes in 3D with u =
     # x+y+z as exact solution
 
@@ -118,7 +121,7 @@ def test_multimesh_poisson_3d():
                      2, 2, 2)
 
     # Solve multimesh Poisson
-    uh = solve_multimesh_poisson(mesh_0, mesh_1, exactsolution_3d())
+    uh = solve_multimesh_poisson(mesh_0, mesh_1, exactsolution_3d)
 
     # Check error
-    assert errornorm(exactsolution_3d(), uh, 'L2', degree_rise=1) < DOLFIN_EPS_LARGE
+    assert errornorm(exactsolution_3d, uh, 'L2', degree_rise=1) < DOLFIN_EPS_LARGE
-- 
2.10.5

