diff options
author | João Valverde <j@v6e.pt> | 2023-06-05 17:47:08 +0100 |
---|---|---|
committer | João Valverde <j@v6e.pt> | 2023-06-05 20:56:20 +0000 |
commit | 7db3c5264aeae7a10fa7952e567d5d3cc7004d87 (patch) | |
tree | cb8a081171d7bc2e2dd9ccd06d131d5a0f251697 /test/fixtures_ws.py | |
parent | 742740b2bb9000bee6bcd24f76cb0815e3a71441 (diff) |
Tests: Remove remaining legacy unittest code
Diffstat (limited to 'test/fixtures_ws.py')
-rw-r--r-- | test/fixtures_ws.py | 83 |
1 files changed, 34 insertions, 49 deletions
diff --git a/test/fixtures_ws.py b/test/fixtures_ws.py index 49db5a0c2a..1d5f5d72ea 100644 --- a/test/fixtures_ws.py +++ b/test/fixtures_ws.py @@ -14,12 +14,9 @@ import subprocess import sys import tempfile import types +import pytest -import fixtures -import subprocesstest - - -@fixtures.fixture(scope='session') +@pytest.fixture(scope='session') def capture_interface(request, cmd_dumpcap): ''' Name of capture interface. Tests will be skipped if dumpcap is not @@ -27,20 +24,20 @@ def capture_interface(request, cmd_dumpcap): ''' disabled = request.config.getoption('--disable-capture', default=False) if disabled: - fixtures.skip('Capture tests are disabled via --disable-capture') + pytest.skip('Capture tests are disabled via --disable-capture') proc = subprocess.Popen((cmd_dumpcap, '-D'), stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) outs, errs = proc.communicate() if proc.returncode != 0: print('"dumpcap -D" exited with %d. stderr:\n%s' % (proc.returncode, errs)) - fixtures.skip('Test requires capture privileges and an interface.') + pytest.skip('Test requires capture privileges and an interface.') # Matches: "lo (Loopback)" (Linux), "lo0 (Loopback)" (macOS) or # "\Device\NPF_{...} (Npcap Loopback Adapter)" (Windows) print('"dumpcap -D" output:\n%s' % (outs,)) m = re.search(r'^(\d+)\. .*\(.*Loopback.*\)', outs, re.MULTILINE|re.IGNORECASE) if not m: - fixtures.skip('Test requires a capture interface.') + pytest.skip('Test requires a capture interface.') iface = m.group(1) # Interface found, check for capture privileges (needed for Linux). try: @@ -52,10 +49,10 @@ def capture_interface(request, cmd_dumpcap): print('"dumpcap -L -i %s" exited with %d. Output:\n%s' % (iface, e.returncode, e.output)) - fixtures.skip('Test requires capture privileges.') + pytest.skip('Test requires capture privileges.') -@fixtures.fixture(scope='session') +@pytest.fixture(scope='session') def program_path(request): ''' Path to the Wireshark binaries as set by the --program-path option, the @@ -77,7 +74,7 @@ def program_path(request): raise AssertionError('Missing directory with Wireshark binaries') -@fixtures.fixture(scope='session') +@pytest.fixture(scope='session') def program(program_path, request): skip_if_missing = request.config.getoption('--skip-missing-programs', default='') @@ -90,65 +87,65 @@ def program(program_path, request): path = os.path.abspath(os.path.join(program_path, name + dotexe)) if not os.access(path, os.X_OK): if skip_if_missing == ['all'] or name in skip_if_missing: - fixtures.skip('Program %s is not available' % (name,)) + pytest.skip('Program %s is not available' % (name,)) raise AssertionError('Program %s is not available' % (name,)) return path return resolver -@fixtures.fixture(scope='session') +@pytest.fixture(scope='session') def cmd_capinfos(program): return program('capinfos') -@fixtures.fixture(scope='session') +@pytest.fixture(scope='session') def cmd_dumpcap(program): return program('dumpcap') -@fixtures.fixture(scope='session') +@pytest.fixture(scope='session') def cmd_mergecap(program): return program('mergecap') -@fixtures.fixture(scope='session') +@pytest.fixture(scope='session') def cmd_rawshark(program): return program('rawshark') -@fixtures.fixture(scope='session') +@pytest.fixture(scope='session') def cmd_tshark(program): return program('tshark') -@fixtures.fixture(scope='session') +@pytest.fixture(scope='session') def cmd_text2pcap(program): return program('text2pcap') -@fixtures.fixture(scope='session') +@pytest.fixture(scope='session') def cmd_editcap(program): return program('editcap') -@fixtures.fixture(scope='session') +@pytest.fixture(scope='session') def cmd_wireshark(program): return program('wireshark') -@fixtures.fixture(scope='session') +@pytest.fixture(scope='session') def wireshark_command(cmd_wireshark): # Windows can always display the GUI and macOS can if we're in a login session. # On Linux, headless mode is used, see QT_QPA_PLATFORM in the 'test_env' fixture. if sys.platform == 'darwin' and 'SECURITYSESSIONID' not in os.environ: - fixtures.skip('Wireshark GUI tests require loginwindow session') + pytest.skip('Wireshark GUI tests require loginwindow session') if sys.platform not in ('win32', 'darwin', 'linux'): if 'DISPLAY' not in os.environ: - fixtures.skip('Wireshark GUI tests require DISPLAY') + pytest.skip('Wireshark GUI tests require DISPLAY') return (cmd_wireshark, '-ogui.update.enabled:FALSE') -@fixtures.fixture(scope='session') +@pytest.fixture(scope='session') def cmd_extcap(program): def extcap_name(name): if sys.platform == 'darwin': @@ -158,7 +155,7 @@ def cmd_extcap(program): return extcap_name -@fixtures.fixture(scope='session') +@pytest.fixture(scope='session') def features(cmd_tshark, make_env): '''Returns an object describing available features in tshark.''' try: @@ -187,7 +184,7 @@ def features(cmd_tshark, make_env): ) -@fixtures.fixture(scope='session') +@pytest.fixture(scope='session') def dirs(): '''Returns fixed directories containing test input.''' this_dir = os.path.dirname(__file__) @@ -202,28 +199,28 @@ def dirs(): ) -@fixtures.fixture(scope='session') +@pytest.fixture(scope='session') def capture_file(dirs): '''Returns the path to a capture file.''' def resolver(filename): return os.path.join(dirs.capture_dir, filename) return resolver -@fixtures.fixture +@pytest.fixture def result_file(tmp_path): '''Returns the path to a temporary file.''' def result_file_real(filename): return str(tmp_path / filename) return result_file_real -@fixtures.fixture +@pytest.fixture def home_path(): '''Per-test home directory, removed when finished.''' with tempfile.TemporaryDirectory(prefix='wireshark-tests-home-') as dirname: yield dirname -@fixtures.fixture +@pytest.fixture def conf_path(home_path): '''Path to the Wireshark configuration directory.''' if sys.platform.startswith('win32'): @@ -234,7 +231,7 @@ def conf_path(home_path): return conf_path -@fixtures.fixture(scope='session') +@pytest.fixture(scope='session') def make_env(): """A factory for a modified environment to ensure reproducible tests.""" def make_env_real(home=None): @@ -256,20 +253,16 @@ def make_env(): return make_env_real -@fixtures.fixture +@pytest.fixture def base_env(home_path, make_env, request): """A modified environment to ensure reproducible tests. Tests can modify this environment as they see fit.""" env = make_env(home=home_path) - # Remove this if test instances no longer inherit from SubprocessTestCase? - if isinstance(request.instance, subprocesstest.SubprocessTestCase): - # Inject the test environment as default if it was not overridden. - request.instance.injected_test_env = env return env -@fixtures.fixture +@pytest.fixture def test_env(base_env, conf_path, request, dirs): '''A process environment with a populated configuration directory.''' # Populate our UAT files @@ -307,14 +300,10 @@ def test_env(base_env, conf_path, request, dirs): # Windows it unfortunately crashes (Qt 5.12.0). env['QT_QPA_PLATFORM'] = 'minimal' - # Remove this if test instances no longer inherit from SubprocessTestCase? - if isinstance(request.instance, subprocesstest.SubprocessTestCase): - # Inject the test environment as default if it was not overridden. - request.instance.injected_test_env = env return env -@fixtures.fixture +@pytest.fixture def test_env_80211_user_tk(base_env, conf_path, request, dirs): '''A process environment with a populated configuration directory.''' # Populate our UAT files @@ -346,13 +335,9 @@ def test_env_80211_user_tk(base_env, conf_path, request, dirs): # Windows it unfortunately crashes (Qt 5.12.0). env['QT_QPA_PLATFORM'] = 'minimal' - # Remove this if test instances no longer inherit from SubprocessTestCase? - if isinstance(request.instance, subprocesstest.SubprocessTestCase): - # Inject the test environment as default if it was not overridden. - request.instance.injected_test_env = env return env -@fixtures.fixture +@pytest.fixture def unicode_env(home_path, make_env): '''A Wireshark configuration directory with Unicode in its path.''' home_env = 'APPDATA' if sys.platform.startswith('win32') else 'HOME' @@ -370,7 +355,7 @@ def unicode_env(home_path, make_env): ) -@fixtures.fixture(scope='session') +@pytest.fixture(scope='session') def make_screenshot(): '''Creates a screenshot and save it to a file. Intended for CI purposes.''' def make_screenshot_real(filename): @@ -387,7 +372,7 @@ def make_screenshot(): return make_screenshot_real -@fixtures.fixture +@pytest.fixture def make_screenshot_on_error(request, make_screenshot, result_file): '''Writes a screenshot when a process times out.''' @contextmanager |