aboutsummaryrefslogtreecommitdiffstats
path: root/test/fixtures.py
diff options
context:
space:
mode:
authorPeter Wu <peter@lekensteyn.nl>2018-11-15 18:44:59 +0100
committerPeter Wu <peter@lekensteyn.nl>2018-11-16 13:55:28 +0000
commit3ab521118a0d068e0d5c795a5a57b13cd7790a75 (patch)
tree72900c817752260ab91945cc85302b78f8eaffe6 /test/fixtures.py
parentcb9be3850da46ca1f706a00b16cdb65a95ce66a0 (diff)
test: convert capture tests to use fixtures, fix tests without dumpcap
Add a new --capture-interface option to pytest, similar to test.py. It will grab some Ethernet interface on Windows. An empty value overrides this and disables capture tests. Remove the test.py --enable-capture option since that is implied by the --capture-interface option. Port the `test.py --program-path` option to pytest and additionally make the pytest look in the current working directory if neither WS_BIN_PATH nor --program-path are specified. Drop config.setProgramPath, this allows tests to be run even if not all binaries are available. With all capture tests converted to fixtures, it is now possible to run tests when Wireshark is not built with libpcap as tests that depend on cmd_dumpcap (or capture_interface) will be skipped. Bug: 14949 Change-Id: Ie802c07904936de4cd30a4c68b6a5139e6680fbd Reviewed-on: https://code.wireshark.org/review/30656 Petri-Dish: Peter Wu <peter@lekensteyn.nl> Tested-by: Petri Dish Buildbot Reviewed-by: Peter Wu <peter@lekensteyn.nl>
Diffstat (limited to 'test/fixtures.py')
-rw-r--r--test/fixtures.py43
1 files changed, 38 insertions, 5 deletions
diff --git a/test/fixtures.py b/test/fixtures.py
index 26c0ed371f..1dbfe40977 100644
--- a/test/fixtures.py
+++ b/test/fixtures.py
@@ -7,6 +7,7 @@
# SPDX-License-Identifier: (GPL-2.0-or-later OR MIT)
#
+import argparse
import functools
import inspect
import sys
@@ -197,10 +198,13 @@ class _ExecutionScope(object):
def cached_result(self, spec):
'''Obtain the cached result for a previously executed fixture.'''
- value, exc = self._find_scope(spec.scope).cache[spec.name]
+ entry = self._find_scope(spec.scope).cache.get(spec.name)
+ if not entry:
+ return None, False
+ value, exc = entry
if exc:
raise exc
- return value
+ return value, True
def _execute_one(self, spec, test_fn):
# A fixture can only execute in the same or earlier scopes
@@ -265,7 +269,15 @@ class _FixtureRequest(object):
if not spec:
assert argname == 'request'
return self
- return self._context.cached_result(spec)
+ value, ok = self._context.cached_result(spec)
+ if not ok:
+ # If getfixturevalue is called directly from a setUp function, the
+ # fixture value might not have computed before, so evaluate it now.
+ # As the test function is not available, use None.
+ self._context.execute(spec, test_fn=None)
+ value, ok = self._context.cached_result(spec)
+ assert ok, 'Failed to execute fixture %s' % (spec,)
+ return value
def destroy(self):
self._context.destroy()
@@ -277,6 +289,11 @@ class _FixtureRequest(object):
def instance(self):
return self.function.__self__
+ @property
+ def config(self):
+ '''The pytest config object associated with this request.'''
+ return _config
+
def _patch_unittest_testcase_class(cls):
'''
@@ -304,8 +321,20 @@ def _patch_unittest_testcase_class(cls):
cls._orig_tearDown, cls.tearDown = cls.tearDown, tearDown
+class _Config(object):
+ def __init__(self, args):
+ assert isinstance(args, argparse.Namespace)
+ self.args = args
+
+ def getoption(self, name, default):
+ '''Partial emulation for pytest Config.getoption.'''
+ name = name.lstrip('-').replace('-', '_')
+ return getattr(self.args, name, default)
+
+
_fallback = None
_session_context = None
+_config = None
def init_fallback_fixtures_once():
@@ -317,10 +346,14 @@ def init_fallback_fixtures_once():
# Register standard fixtures here as needed
-def create_session():
- global _session_context
+def create_session(args=None):
+ '''Start a test session where args is from argparse.'''
+ global _session_context, _config
assert not _use_native_pytest
_session_context = _ExecutionScope('session', None)
+ if args is None:
+ args = argparse.Namespace()
+ _config = _Config(args)
def destroy_session():