From 9b264c75145cd854a1c0d0df9383a4a9f78f544b Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Sun, 28 Jun 2015 22:58:48 +0200 Subject: [PATCH] tests: Share common markers. --- .../browser/http/test_content_disposition.py | 8 +++---- tests/conftest.py | 21 +++++++++++++++++++ tests/misc/test_guiprocess.py | 14 +++++-------- tests/utils/test_qtutils.py | 5 ++--- tests/utils/test_standarddir.py | 6 ++---- tests/utils/test_version.py | 15 ++++++------- tox.ini | 6 ++++++ 7 files changed, 46 insertions(+), 29 deletions(-) diff --git a/tests/browser/http/test_content_disposition.py b/tests/browser/http/test_content_disposition.py index 576998bcf..ea000d5c0 100644 --- a/tests/browser/http/test_content_disposition.py +++ b/tests/browser/http/test_content_disposition.py @@ -586,7 +586,7 @@ class TestAttachment: header_checker.check_filename('attachment; filename="/foo.html"', 'foo.html') - @pytest.mark.skipif(os.name != 'posix', reason="requires POSIX") + @pytest.mark.posix def test_attabspathwin_unix(self, header_checker): """'attachment', specifying an absolute filename in the fs root. @@ -601,7 +601,7 @@ class TestAttachment: header_checker.check_filename(r'attachment; filename="\\foo.html"', r'\foo.html') - @pytest.mark.skipif(os.name != 'nt', reason="requires Windows") + @pytest.mark.windows def test_attabspathwin_win(self, header_checker): """'attachment', specifying an absolute filename in the fs root. @@ -791,7 +791,7 @@ class TestCharacterSet: "attachment; filename*=UTF-8''A-%2541.html", 'A-%41.html') - @pytest.mark.skipif(os.name != 'posix', reason="requires POSIX") + @pytest.mark.posix def test_attwithfn2231abspathdisguised_unix(self, header_checker): r"""'attachment', specifying a filename of \foo.html. @@ -801,7 +801,7 @@ class TestCharacterSet: "attachment; filename*=UTF-8''%5cfoo.html", r'\foo.html') - @pytest.mark.skipif(os.name != 'nt', reason="requires Windows") + @pytest.mark.windows def test_attwithfn2231abspathdisguised_win(self, header_checker): r"""'attachment', specifying a filename of \foo.html. diff --git a/tests/conftest.py b/tests/conftest.py index ca7042232..67e960102 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -19,6 +19,8 @@ """The qutebrowser test suite contest file.""" +import os +import sys import collections import itertools @@ -196,3 +198,22 @@ def config_stub(stubs): objreg.register('config', stub) yield stub objreg.delete('config') + + +def pytest_runtest_setup(item): + """Add some custom markers.""" + if not isinstance(item, item.Function): + return + + if item.get_marker('posix') and os.name != 'posix': + pytest.skip("Requires a POSIX os.") + elif item.get_marker('windows') and os.name != 'nt': + pytest.skip("Requires Windows.") + elif item.get_marker('linux') and not sys.platform.startswith('linux'): + pytest.skip("Requires Linux.") + elif item.get_marker('osx') and os.name != 'darwin': + pytest.skip("Requires OS X.") + elif item.get_marker('not_frozen') and getattr(sys, 'frozen', False): + pytest.skip("Can't be run when frozen!") + elif item.get_marker('frozen') and not getattr(sys, 'frozen', False): + pytest.skip("Can only run when frozen!") diff --git a/tests/misc/test_guiprocess.py b/tests/misc/test_guiprocess.py index 9c28ab9f0..eccd0da62 100644 --- a/tests/misc/test_guiprocess.py +++ b/tests/misc/test_guiprocess.py @@ -33,10 +33,6 @@ from qutebrowser.misc import guiprocess # FIXME check statusbar messages -no_frozen = pytest.mark.skipif( - getattr(sys, 'frozen', False), reason="Can't be executed when frozen.") - - def _py_proc(code): """Get a python executable and args list which executes the given code.""" return (sys.executable, ['-c', textwrap.dedent(code.strip('\n'))]) @@ -68,7 +64,7 @@ def fake_proc(monkeypatch, stubs): return p -@no_frozen +@pytest.mark.not_frozen def test_start(proc, qtbot): """Test simply starting a process.""" with qtbot.waitSignals([proc.started, proc.finished], raising=True, @@ -80,7 +76,7 @@ def test_start(proc, qtbot): @pytest.mark.parametrize('argv', [ - no_frozen(_py_proc('import sys; sys.exit(0)')), + pytest.mark.not_frozen(_py_proc('import sys; sys.exit(0)')), ('does_not', 'exist'), ]) def test_start_detached(fake_proc, argv): @@ -90,7 +86,7 @@ def test_start_detached(fake_proc, argv): fake_proc._proc.startDetached.assert_called_with(*list(argv) + [None]) -@no_frozen +@pytest.mark.not_frozen def test_double_start(qtbot, proc): """Test starting a GUIProcess twice.""" with qtbot.waitSignal(proc.started, raising=True, timeout=2000): @@ -100,7 +96,7 @@ def test_double_start(qtbot, proc): proc.start('', []) -@no_frozen +@pytest.mark.not_frozen def test_double_start_finished(qtbot, proc): """Test starting a GUIProcess twice (with the first call finished).""" with qtbot.waitSignals([proc.started, proc.finished], raising=True, @@ -127,7 +123,7 @@ def test_error(qtbot, proc): proc.start('this_does_not_exist_either', []) -@no_frozen +@pytest.mark.not_frozen def test_exit_unsuccessful(qtbot, proc): with qtbot.waitSignal(proc.finished, raising=True, timeout=2000): proc.start(*_py_proc('import sys; sys.exit(0)')) diff --git a/tests/utils/test_qtutils.py b/tests/utils/test_qtutils.py index aaec77870..d4921279b 100644 --- a/tests/utils/test_qtutils.py +++ b/tests/utils/test_qtutils.py @@ -757,8 +757,7 @@ class TestPyQIODevice: with pytest.raises(io.UnsupportedOperation): pyqiodev.seek(0, whence) - @pytest.mark.skipif(getattr(sys, 'frozen', False), - reason="Can't be executed when frozen.") + @pytest.mark.not_frozen def test_qprocess(self): """Test PyQIODevice with a QProcess which is non-sequential. @@ -872,7 +871,7 @@ class TestPyQIODevice: pyqiodev_failing.write(b'x') assert str(excinfo.value) == 'Writing failed' - @pytest.mark.skipif(os.name != 'posix', reason="Needs a POSIX OS.") + @pytest.mark.posix @pytest.mark.skipif(not os.path.exists('/dev/full'), reason="Needs /dev/full.") def test_write_error_real(self): diff --git a/tests/utils/test_standarddir.py b/tests/utils/test_standarddir.py index 02bcc02d6..43d2beeca 100644 --- a/tests/utils/test_standarddir.py +++ b/tests/utils/test_standarddir.py @@ -101,8 +101,7 @@ class TestWritableLocation: assert '\\' in loc -@pytest.mark.skipif(not sys.platform.startswith("linux"), - reason="requires Linux") +@pytest.mark.linux @pytest.mark.usefixtures('no_cachedir_tag') class TestGetStandardDirLinux: @@ -145,8 +144,7 @@ class TestGetStandardDirLinux: assert standarddir.cache() == expected -@pytest.mark.skipif(not sys.platform.startswith("win"), - reason="requires Windows") +@pytest.mark.windows @pytest.mark.usefixtures('no_cachedir_tag') class TestGetStandardDirWindows: diff --git a/tests/utils/test_version.py b/tests/utils/test_version.py index 3e21a57c1..4fdc4532d 100644 --- a/tests/utils/test_version.py +++ b/tests/utils/test_version.py @@ -112,15 +112,13 @@ class TestGitStr: commit_file_mock.side_effect = OSError assert version._git_str() is None - @pytest.mark.skipif(getattr(sys, 'frozen', False), - reason="Can't be executed when frozen!") + @pytest.mark.not_frozen def test_normal_successful(self, git_str_subprocess_fake): """Test with git returning a successful result.""" git_str_subprocess_fake.retval = 'c0ffeebabe' assert version._git_str() == 'c0ffeebabe' - @pytest.mark.skipif(not getattr(sys, 'frozen', False), - reason="Can only executed when frozen!") + @pytest.mark.frozen def test_normal_successful_frozen(self, git_str_subprocess_fake): """Test with git returning a successful result.""" # The value is defined in scripts/freeze_tests.py. @@ -140,8 +138,7 @@ class TestGitStr: side_effect=OSError) assert version._git_str() is None - @pytest.mark.skipif(getattr(sys, 'frozen', False), - reason="Can't be executed when frozen!") + @pytest.mark.not_frozen def test_normal_path_nofile(self, monkeypatch, caplog, git_str_subprocess_fake, commit_file_mock): """Test with undefined __file__ but available git-commit-id.""" @@ -511,17 +508,17 @@ class TestOsInfo: expected = ['OS Version: ?'] assert ret == expected - @pytest.mark.skipif(sys.platform != 'linux', reason="requires Linux") + @pytest.mark.linux def test_linux_real(self): """Make sure there are no exceptions with a real Linux.""" version._os_info() - @pytest.mark.skipif(sys.platform != 'win32', reason="requires Windows") + @pytest.mark.windows def test_windows_real(self): """Make sure there are no exceptions with a real Windows.""" version._os_info() - @pytest.mark.skipif(sys.platform != 'darwin', reason="requires OS X") + @pytest.mark.osx def test_os_x_real(self): """Make sure there are no exceptions with a real OS X.""" version._os_info() diff --git a/tox.ini b/tox.ini index 01291f9f6..1682498b2 100644 --- a/tox.ini +++ b/tox.ini @@ -184,6 +184,12 @@ commands = norecursedirs = .tox .venv markers = gui: Tests using the GUI (e.g. spawning widgets) + posix: Tests which only can run on a POSIX OS. + windows: Tests which only can run on Windows. + linux: Tests which only can run on Linux. + osx: Tests which only can run on OS X. + not_frozen: Tests which can't be run if sys.frozen is True. + frozen: Tests which can only be run if sys.frozen is True. flakes-ignore = UnusedImport UnusedVariable