Bug 1748737: Update deprecated pytest code r=webdriver-reviewers,ahal,whimboo

`pytest` is about to be upgraded, so this patch resolves a bunch of the
issues currently blocking the upgrade.
Most changes done in this patch are described in the "deprecations" page
on the pytest docs:
https://docs.pytest.org/en/latest/deprecations.html#using-class-in-custom-collectors

One undocumented change is that defining a pytest argument as the same name as an
in-scope, imported fixture function, will no longer use that imported function.

Differential Revision: https://phabricator.services.mozilla.com/D136537
This commit is contained in:
Mitchell Hentges 2022-01-21 18:21:55 +00:00
parent 5a313efe20
commit 0116a8d491
9 changed files with 45 additions and 34 deletions

View file

@ -313,7 +313,7 @@ def main(*args, **kwargs):
args.extend(
[
"--rootdir",
topsrcdir,
str(topsrcdir),
"-c",
os.path.join(here, "pytest.ini"),
"-vv",

View file

@ -18,7 +18,7 @@ except ImportError:
import mozinfo
import pytest
from manifestparser import expression
from moztest.selftest.fixtures import binary, setup_test_harness # noqa
from moztest.selftest.fixtures import binary_fixture, setup_test_harness # noqa
here = os.path.abspath(os.path.dirname(__file__))
setup_args = [False, "reftest", "reftest"]
@ -142,7 +142,7 @@ def skip_using_mozinfo(request, setup_test_harness):
runreftest = pytest.importorskip("runreftest")
runreftest.update_mozinfo()
skip_mozinfo = request.node.get_marker("skip_mozinfo")
skip_mozinfo = request.node.get_closest_marker("skip_mozinfo")
if skip_mozinfo:
value = skip_mozinfo.args[0]
if expression.parse(value, **mozinfo.info):

View file

@ -117,7 +117,10 @@ disperse_tasks = list(
def idfn(param):
if isinstance(param, tuple):
return param[0].__name__
try:
return param[0].__name__
except AttributeError:
return None
return None

View file

@ -97,5 +97,5 @@ def mock_marionette(request):
"""Mock marionette instance"""
marionette = MagicMock(spec=dir(Marionette()))
if "has_crashed" in request.funcargnames:
marionette.check_for_crash.return_value = request.getfuncargvalue("has_crashed")
marionette.check_for_crash.return_value = request.getfixturevalue("has_crashed")
return marionette

View file

@ -22,7 +22,7 @@ def harness_class(request):
failures according to the supplied test parameter
"""
if "num_fails_crashed" in request.funcargnames:
num_fails_crashed = request.getfuncargvalue("num_fails_crashed")
num_fails_crashed = request.getfixturevalue("num_fails_crashed")
else:
num_fails_crashed = (0, 0)
harness_cls = Mock(spec=MarionetteHarness)
@ -41,7 +41,7 @@ def runner_class(request):
runner.crashed attributes are provided by a test parameter
"""
if "num_fails_crashed" in request.funcargnames:
failures, crashed = request.getfuncargvalue("num_fails_crashed")
failures, crashed = request.getfixturevalue("num_fails_crashed")
else:
failures = 0
crashed = 0

View file

@ -12,7 +12,7 @@ import pytest
import mozinfo
from manifestparser import TestManifest, expression
from moztest.selftest.fixtures import binary, setup_test_harness # noqa
from moztest.selftest.fixtures import binary_fixture, setup_test_harness # noqa
here = os.path.abspath(os.path.dirname(__file__))
setup_args = [os.path.join(here, "files"), "mochitest", "testing/mochitest"]
@ -140,7 +140,7 @@ def skip_using_mozinfo(request, setup_test_harness):
runtests = pytest.importorskip("runtests")
runtests.update_mozinfo()
skip_mozinfo = request.node.get_marker("skip_mozinfo")
skip_mozinfo = request.node.get_closest_marker("skip_mozinfo")
if skip_mozinfo:
value = skip_mozinfo.args[0]
if expression.parse(value, **mozinfo.info):

View file

@ -50,7 +50,6 @@ def check_for_java_exception():
return wrapper
@pytest.fixture
def minidump_files(request, tmpdir):
files = []
@ -104,6 +103,11 @@ def minidump_files(request, tmpdir):
return files
@pytest.fixture(name="minidump_files")
def minidump_files_fixture(request, tmpdir):
return minidump_files(request, tmpdir)
@pytest.fixture(autouse=True)
def mock_popen(monkeypatch):
"""Generate a class that can mock subprocess.Popen.

View file

@ -90,7 +90,6 @@ def setup_test_harness(request, flavor="plain"):
return inner
@pytest.fixture(scope="session")
def binary():
"""Return a Firefox binary"""
try:
@ -108,3 +107,8 @@ def binary():
if "GECKO_BINARY_PATH" in os.environ:
return os.environ["GECKO_BINARY_PATH"]
@pytest.fixture(name="binary", scope="session")
def binary_fixture():
return binary()

View file

@ -12,7 +12,7 @@ import shutil
import mozunit
import pytest
from moztest.selftest.fixtures import binary as real_binary # noqa: F401
from moztest.selftest.fixtures import binary_fixture # noqa: F401
from mozversion import errors, get_version
@ -20,8 +20,8 @@ from mozversion import errors, get_version
"""test getting application version information from a binary path"""
@pytest.fixture(name="binary")
def fixure_binary(tmpdir):
@pytest.fixture()
def fake_binary(tmpdir):
binary = tmpdir.join("binary")
binary.write("foobar")
return str(binary)
@ -57,46 +57,46 @@ SourceRepository = PlatformSourceRepo"""
return str(ini)
def test_real_binary(real_binary): # noqa: F811
if not real_binary:
def test_real_binary(binary): # noqa: F811
if not binary:
pytest.skip("No binary found")
v = get_version(real_binary)
v = get_version(binary)
assert isinstance(v, dict)
def test_binary(binary, application_ini, platform_ini):
_check_version(get_version(binary))
def test_binary(fake_binary, application_ini, platform_ini):
_check_version(get_version(fake_binary))
@pytest.mark.skipif(
not hasattr(os, "symlink"), reason="os.symlink not supported on this platform"
)
def test_symlinked_binary(binary, application_ini, platform_ini, tmpdir):
def test_symlinked_binary(fake_binary, application_ini, platform_ini, tmpdir):
# create a symlink of the binary in another directory and check
# version against this symlink
symlink = str(tmpdir.join("symlink"))
os.symlink(binary, symlink)
os.symlink(fake_binary, symlink)
_check_version(get_version(symlink))
def test_binary_in_current_path(binary, application_ini, platform_ini, tmpdir):
def test_binary_in_current_path(fake_binary, application_ini, platform_ini, tmpdir):
os.chdir(str(tmpdir))
_check_version(get_version())
def test_with_ini_files_on_osx(
binary, application_ini, platform_ini, monkeypatch, tmpdir
fake_binary, application_ini, platform_ini, monkeypatch, tmpdir
):
monkeypatch.setattr(sys, "platform", "darwin")
# get_version is working with ini files next to the binary
_check_version(get_version(binary=binary))
_check_version(get_version(binary=fake_binary))
# or if they are in the Resources dir
# in this case the binary must be in a Contents dir, next
# to the Resources dir
contents_dir = tmpdir.mkdir("Contents")
moved_binary = str(contents_dir.join(os.path.basename(binary)))
shutil.move(binary, moved_binary)
moved_binary = str(contents_dir.join(os.path.basename(fake_binary)))
shutil.move(fake_binary, moved_binary)
resources_dir = str(tmpdir.mkdir("Resources"))
shutil.move(application_ini, resources_dir)
@ -110,22 +110,22 @@ def test_invalid_binary_path(tmpdir):
get_version(str(tmpdir.join("invalid")))
def test_without_ini_files(binary):
def test_without_ini_files(fake_binary):
"""With missing ini files an exception should be thrown"""
with pytest.raises(errors.AppNotFoundError):
get_version(binary)
get_version(fake_binary)
def test_without_platform_ini_file(binary, application_ini):
def test_without_platform_ini_file(fake_binary, application_ini):
"""With a missing platform.ini file an exception should be thrown"""
with pytest.raises(errors.AppNotFoundError):
get_version(binary)
get_version(fake_binary)
def test_without_application_ini_file(binary, platform_ini):
def test_without_application_ini_file(fake_binary, platform_ini):
"""With a missing application.ini file an exception should be thrown"""
with pytest.raises(errors.AppNotFoundError):
get_version(binary)
get_version(fake_binary)
def test_with_exe(application_ini, platform_ini, tmpdir):
@ -135,9 +135,9 @@ def test_with_exe(application_ini, platform_ini, tmpdir):
_check_version(get_version(os.path.splitext(str(binary))[0]))
def test_not_found_with_binary_specified(binary):
def test_not_found_with_binary_specified(fake_binary):
with pytest.raises(errors.LocalAppNotFoundError):
get_version(binary)
get_version(fake_binary)
def _check_version(version):