diff --git a/tests/test_make.py b/tests/test_make.py index 286f18af3..45c35df6a 100644 --- a/tests/test_make.py +++ b/tests/test_make.py @@ -58,6 +58,19 @@ attrs_st = simple_attrs.map(lambda c: Attribute.from_counting_attr("name", c)) +@pytest.fixture(name="with_and_without_validation", params=[True, False]) +def _with_and_without_validation(request): + """ + Run tests with and without validation enabled. + """ + attr.validators.set_disabled(request.param) + + try: + yield + finally: + attr.validators.set_disabled(False) + + class TestCountingAttr: """ Tests for `attr`. @@ -612,8 +625,8 @@ class D: assert C.D.__name__ == "D" assert C.D.__qualname__ == C.__qualname__ + ".D" - @pytest.mark.parametrize("with_validation", [True, False]) - def test_pre_init(self, with_validation): + @pytest.mark.usefixtures("with_and_without_validation") + def test_pre_init(self): """ Verify that __attrs_pre_init__ gets called if defined. """ @@ -623,16 +636,12 @@ class C: def __attrs_pre_init__(self2): self2.z = 30 - try: - attr.validators.set_disabled(not with_validation) - c = C() - finally: - attr.validators.set_disabled(False) + c = C() assert 30 == getattr(c, "z", None) - @pytest.mark.parametrize("with_validation", [True, False]) - def test_pre_init_args(self, with_validation): + @pytest.mark.usefixtures("with_and_without_validation") + def test_pre_init_args(self): """ Verify that __attrs_pre_init__ gets called with extra args if defined. """ @@ -644,18 +653,15 @@ class C: def __attrs_pre_init__(self2, x): self2.z = x + 1 - try: - attr.validators.set_disabled(not with_validation) - c = C(x=10) - finally: - attr.validators.set_disabled(False) + c = C(x=10) assert 11 == getattr(c, "z", None) - @pytest.mark.parametrize("with_validation", [True, False]) - def test_pre_init_kwargs(self, with_validation): + @pytest.mark.usefixtures("with_and_without_validation") + def test_pre_init_kwargs(self): """ - Verify that __attrs_pre_init__ gets called with extra args and kwargs if defined. + Verify that __attrs_pre_init__ gets called with extra args and kwargs + if defined. """ @attr.s @@ -666,16 +672,12 @@ class C: def __attrs_pre_init__(self2, x, y): self2.z = x + y + 1 - try: - attr.validators.set_disabled(not with_validation) - c = C(10, y=11) - finally: - attr.validators.set_disabled(False) + c = C(10, y=11) assert 22 == getattr(c, "z", None) - @pytest.mark.parametrize("with_validation", [True, False]) - def test_pre_init_kwargs_only(self, with_validation): + @pytest.mark.usefixtures("with_and_without_validation") + def test_pre_init_kwargs_only(self): """ Verify that __attrs_pre_init__ gets called with extra kwargs only if defined. @@ -688,20 +690,15 @@ class C: def __attrs_pre_init__(self2, y): self2.z = y + 1 - try: - attr.validators.set_disabled(not with_validation) - c = C(y=11) - finally: - attr.validators.set_disabled(False) + c = C(y=11) assert 12 == getattr(c, "z", None) - @pytest.mark.parametrize("with_validation", [True, False]) - def test_post_init(self, with_validation, monkeypatch): + @pytest.mark.usefixtures("with_and_without_validation") + def test_post_init(self): """ Verify that __attrs_post_init__ gets called if defined. """ - monkeypatch.setattr(_config, "_run_validators", with_validation) @attr.s class C: @@ -715,12 +712,11 @@ def __attrs_post_init__(self2): assert 30 == getattr(c, "z", None) - @pytest.mark.parametrize("with_validation", [True, False]) - def test_pre_post_init_order(self, with_validation, monkeypatch): + @pytest.mark.usefixtures("with_and_without_validation") + def test_pre_post_init_order(self): """ Verify that __attrs_post_init__ gets called if defined. """ - monkeypatch.setattr(_config, "_run_validators", with_validation) @attr.s class C: