From cdffe59ac62e6e7dd0240e54f4952f271df46230 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Mon, 5 Apr 2021 17:24:54 -0400 Subject: [PATCH] Add test to ensure tests are hermetic w.r.t. profiles and add fixture to make hermetic tests that alter profiles. Fixes #4. --- conftest.py | 21 +++++++++++++++++++++ cssutils/tests/test_profiles.py | 1 + examples/website.py | 4 ++-- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/conftest.py b/conftest.py index b2065532..425740dd 100644 --- a/conftest.py +++ b/conftest.py @@ -1,4 +1,25 @@ +import pytest + +import cssutils + + collect_ignore = [ 'cssutils/_fetchgae.py', 'tools', ] + + +@pytest.fixture(autouse=True) +def hermetic_profiles(): + """ + Ensure that tests are hermetic w.r.t. profiles. + """ + before = list(cssutils.profile.profiles) + yield + assert before == cssutils.profile.profiles + + +@pytest.fixture +def saved_profiles(monkeypatch): + profiles = cssutils.profiles.Profiles(log=cssutils.log) + monkeypatch.setattr(cssutils, 'profile', profiles) diff --git a/cssutils/tests/test_profiles.py b/cssutils/tests/test_profiles.py index c9976c59..1348bc00 100644 --- a/cssutils/tests/test_profiles.py +++ b/cssutils/tests/test_profiles.py @@ -78,6 +78,7 @@ def check(*results): # restore cssutils.profile = saved + @pytest.mark.usefixtures('saved_profiles') def test_addProfile(self): "Profiles.addProfile with custom validation function" # unknown profile diff --git a/examples/website.py b/examples/website.py index f41c1618..0c74d1be 100644 --- a/examples/website.py +++ b/examples/website.py @@ -11,8 +11,7 @@ def profile(): """ >>> sheet = cssutils.parseString('x { -test-custommacro: x }') - The following fails in a full suite test run due to non-hermetic tests. - >>> print(sheet.cssRules[0].style.getProperties()[0].valid) # doctest: +SKIP + >>> print(sheet.cssRules[0].style.getProperties()[0].valid) False >>> M1 = { ... 'testvalue': 'x' @@ -24,6 +23,7 @@ def profile(): ... # custom validation function ... '-test-funcval': lambda v: int(v) > 0 ... } + >>> profiles = getfixture('saved_profiles') # keep test hermetic >>> cssutils.profile.addProfile('test', P1, M1) >>> sheet = cssutils.parseString('x { -test-custommacro: x }') >>> print(sheet.cssRules[0].style.getProperties()[0].valid)