Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update pytest #901

Merged
merged 10 commits into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ jobs:
cache-name: cache-${{ matrix.TOXENV }}
with:
path: .tox
key: ${{ runner.os }}-tox-${{ env.cache-name }}-${{ hashFiles('pyproject.toml', 'requirements.in') }}
key: ${{ runner.os }}-tox-${{ env.cache-name }}-${{ hashFiles('pyproject.toml', 'constraints.txt') }}

- uses: actions/cache@v3
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('pyproject.toml', 'requirements.in') }}
key: ${{ runner.os }}-pip-${{ hashFiles('pyproject.toml', 'constraints.txt') }}
restore-keys: |
${{ runner.os }}-pip-

Expand Down Expand Up @@ -111,12 +111,12 @@ jobs:
cache-name: cache-${{ matrix.TOXENV }}
with:
path: .tox
key: ${{ runner.os }}-tox-${{ env.cache-name }}-${{ hashFiles('pyproject.toml', 'requirements.in') }}
key: ${{ runner.os }}-tox-${{ env.cache-name }}-${{ hashFiles('pyproject.toml', 'constraints.txt') }}

- uses: actions/cache@v3
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('pyproject.toml', 'requirements.in') }}
key: ${{ runner.os }}-pip-${{ hashFiles('pyproject.toml', 'constraints.txt') }}
restore-keys: |
${{ runner.os }}-pip-

Expand Down
3 changes: 1 addition & 2 deletions constraints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ attrs==23.2.0
# via
# allure-python-commons
# jsonschema
# pytest
# referencing
babel==2.14.0
# via sphinx
Expand Down Expand Up @@ -239,7 +238,7 @@ pyproject-api==1.6.1
# via tox
pyproject-hooks==1.0.0
# via build
pytest==7.2.2
pytest==7.4.4
# via
# allure-pytest
# pytest-cov
Expand Down
8 changes: 4 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ dependencies = [
"paho-mqtt>=1.3.1,<=1.6.1",
"pyjwt>=2.5.0,<3",
"pykwalify>=1.8.0,<2",
"pytest>=7,<7.3",
"pytest>=7.4,<8",
"python-box>=6,<7",
"requests>=2.22.0,<3",
"stevedore>=4,<5",
]

requires-python = ">=3.8"
requires-python = ">=3.10"

[[project.authors]]
name = "Michael Boulton"
Expand Down Expand Up @@ -137,8 +137,8 @@ source = ["tavern"]
[tool.coverage.paths]
tavern = [
"tavern/",
".tox/py39-generic/lib/python3.9/site-packages/tavern/",
".tox/py39-mqtt/lib/python3.9/site-packages/tavern",
".tox/py311-generic/lib/python3.11/site-packages/tavern/",
".tox/py311-mqtt/lib/python3.11/site-packages/tavern",
]

[tool.pytest.ini_options]
Expand Down
7 changes: 3 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ attrs==23.2.0 \
# via
# allure-python-commons
# jsonschema
# pytest
# referencing
babel==2.14.0 \
--hash=sha256:6919867db036398ba21eb5c7a0f6b28ab8cbc3ae7a73a44ebe34ae74a4e7d363 \
Expand Down Expand Up @@ -878,9 +877,9 @@ pyproject-hooks==1.0.0 \
--hash=sha256:283c11acd6b928d2f6a7c73fa0d01cb2bdc5f07c57a2eeb6e83d5e56b97976f8 \
--hash=sha256:f271b298b97f5955d53fb12b72c1fb1948c22c1a6b70b315c54cedaca0264ef5
# via build
pytest==7.2.2 \
--hash=sha256:130328f552dcfac0b1cec75c12e3f005619dc5f874f0a06e8ff7263f0ee6225e \
--hash=sha256:c99ab0c73aceb050f68929bc93af19ab6db0558791c6a0715723abe9d0ade9d4
pytest==7.4.4 \
--hash=sha256:2cf0005922c6ace4a3e2ec8b4080eb0d9753fdc93107415332f50ce9e7994280 \
--hash=sha256:b090cdf5ed60bf4c45261be03239c2c1c22df034fbffe691abe93cd80cea01d8
# via
# allure-pytest
# pytest-cov
Expand Down
15 changes: 9 additions & 6 deletions tavern/_core/pytest/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
import functools
import itertools
import logging
from typing import Dict, Iterator, List, Mapping
from typing import Any, Dict, Iterator, List, Mapping, Tuple

import pytest
import yaml
from box import Box
from pytest import MarkDecorator

from tavern._core import exceptions
from tavern._core.dict_util import deep_dict_merge, format_keys, get_tavern_box
Expand All @@ -22,15 +23,17 @@
_format_without_inner = functools.partial(format_keys, no_double_format=False)


def _format_test_marks(original_marks, fmt_vars, test_name):
def _format_test_marks(
original_marks: List[Any], fmt_vars: Mapping, test_name: str
) -> Tuple[List[MarkDecorator], Any]:
"""Given the 'raw' marks from the test and any available format variables,
generate new marks for this test

Args:
original_marks (list): Raw string from test - should correspond to either a
original_marks: Raw string from test - should correspond to either a
pytest builtin mark or a custom user mark
fmt_vars (dict): dictionary containing available format variables
test_name (str): Name of test (for error logging)
fmt_vars: dictionary containing available format variables
test_name: Name of test (for error logging)

Returns:
tuple: first element is normal pytest mark objects, second element is all
Expand Down Expand Up @@ -167,7 +170,7 @@ def _get_parametrized_items(
parent: pytest.File,
test_spec: Dict,
parametrize_marks: List[Dict],
pytest_marks: List[pytest.Mark],
pytest_marks: List[MarkDecorator],
) -> Iterator[YamlItem]:
"""Return new items with new format values available based on the mark

Expand Down
24 changes: 16 additions & 8 deletions tavern/_core/pytest/item.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import dataclasses
import logging
import pathlib
from typing import MutableMapping, Optional, Tuple
from typing import Iterable, MutableMapping, Optional, Tuple

import attr
import pytest
import yaml
from _pytest._code.code import ExceptionInfo
from _pytest.nodes import Node
from pytest import Mark, MarkDecorator

from tavern._core import exceptions
from tavern._core.loader import error_on_empty_scalar
Expand Down Expand Up @@ -113,7 +113,7 @@ def fakefun():
def _obj(self):
return self.obj

def add_markers(self, pytest_marks) -> None:
def add_markers(self, pytest_marks: Iterable[MarkDecorator]) -> None:
for pm in pytest_marks:
if pm.name == "usefixtures":
if (
Expand All @@ -129,9 +129,17 @@ def add_markers(self, pytest_marks) -> None:
# usefixtures, which pytest then wraps in a tuple. we need to
# extract this tuple so pytest can use both fixtures.
if isinstance(pm.mark.args[0], (list, tuple)):
new_mark = attr.evolve(pm.mark, args=pm.mark.args[0])
pm = attr.evolve(pm, mark=new_mark)
elif isinstance(pm.mark.args[0], (dict)):
new_mark = Mark(
name=pm.mark.name,
args=tuple(pm.mark.args[0]),
kwargs=pm.mark.kwargs,
param_ids_from=pm.mark._param_ids_from,
param_ids_generated=pm.mark._param_ids_generated,
_ispytest=True,
)

pm = dataclasses.replace(pm, mark=new_mark, _ispytest=True) # type:ignore
elif isinstance(pm.mark.args[0], (dict,)):
# We could raise a TypeError here instead, but then it's a
# failure at collection time (which is a bit annoying to
# deal with). Instead just don't add the marker and it will
Expand Down Expand Up @@ -252,7 +260,7 @@ def runtest(self) -> None:
)

def repr_failure(
self, excinfo: ExceptionInfo[BaseException], style: Optional[str] = None
self, excinfo: pytest.ExceptionInfo[BaseException], style: Optional[str] = None
):
"""called when self.runtest() raises an exception.

Expand Down