Skip to content

Commit

Permalink
Fix unknown pytest options warning (#43)
Browse files Browse the repository at this point in the history
* fix warnings

* Fix mypy in tests

* Add periods to helps

* add test for ini option

* fix ini test

* fix test run name

* Fix tests

* Fix CI

* Remove useless default

* fix tests

* Add Changelog

Co-authored-by: Guido Schmitz <guido.schmitz@devolo.de>
Co-authored-by: Guido Schmitz <guido.schmitz@fedaix.de>
  • Loading branch information
3 people authored Jun 7, 2022
1 parent 9731a62 commit 85bb70a
Show file tree
Hide file tree
Showing 15 changed files with 117 additions and 104 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [5.7.1] - 2022/06/07

### Fixed

* Settings are properly defined to avoid warnings

## [5.7.0] - 2022/05/31

### Added
Expand Down
25 changes: 20 additions & 5 deletions pytest_adaptavist/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import getpass
import logging
import os
from datetime import datetime
from importlib.metadata import PackageNotFoundError, version
from typing import Any, Literal, NoReturn

Expand Down Expand Up @@ -63,31 +64,31 @@ def add_option_ini(
"--restrict-branch-name",
dest="restrict_branch_name",
default="origin/master",
help="Branch to restrict to (default: origin/master)",
help="Branch to restrict to (default: origin/master).",
)
add_option_ini(
"--test_run_name",
dest="test_run_name",
default=TEST_CYCLE_NAME_DEFAULT,
help="Specify test run name (default: <project_key> <test_run_suffix>)",
help="Specify test run name (default: <project_key> <test_run_suffix>).",
) # deprecated
add_option_ini(
"--test_plan_name",
dest="test_plan_name_deprecated",
default=TEST_PLAN_NAME_DEFAULT,
help="Specify test plan name (default: <project_key> <test_plan_suffix>)",
help="Specify test plan name (default: <project_key> <test_plan_suffix>).",
) # deprecated
add_option_ini(
"--test-cycle-name",
dest="test_cycle_name",
default=TEST_CYCLE_NAME_DEFAULT,
help="Specify test cycle name (default: <project_key> <test_run_suffix>)",
help="Specify test cycle name (default: <project_key> <test_run_suffix>).",
)
add_option_ini(
"--test-plan-name",
dest="test_plan_name",
default=TEST_PLAN_NAME_DEFAULT,
help="Specify test plan name (default: <project_key> <test_plan_suffix>)",
help="Specify test plan name (default: <project_key> <test_plan_suffix>).",
)
add_option_ini(
"--append-to-cycle",
Expand All @@ -96,6 +97,20 @@ def add_option_ini(
option_type="bool",
help="Append found test cases to test cycle instead of skipping it.",
)
parser.addini("project_key", help="Specify project key.")
parser.addini("test_plan_key", help="Specify test plan key.")
parser.addini("test_plan_folder", help="Specify test plan folder.")
parser.addini("test_plan_suffix", help="Specify test plan suffix.")
parser.addini("test_run_key", help="Specify test run key.")
parser.addini("test_run_folder", help="Specify test run folder.")
parser.addini(
"test_run_suffix",
help=f"Specify test run suffix (default: test run {datetime.now().strftime('%Y%m%d%H%M')}).",
)
parser.addini("test_environment", help="Specify test environment.")
parser.addini("test_case_keys", help="Specify test case keys.")
parser.addini("test_case_order", help="Specify test case order.")
parser.addini("test_case_range", help="Specify test case range.")


@pytest.hookimpl(trylast=True)
Expand Down
12 changes: 6 additions & 6 deletions pytest_adaptavist/_pytest_adaptavist.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,17 @@ def atm_configure(self):
self.test_case_range = [x.strip() for x in entry.split(",")] if isinstance(entry, str) else entry

if not self.test_plan_key:
self.test_plan_key = self.cfg.get("test_plan_key", None)
self.test_plan_key = self.cfg.get("test_plan_key")
if not self.test_plan_folder:
self.test_plan_folder = self.cfg.get("test_plan_folder", None)
self.test_plan_folder = self.cfg.get("test_plan_folder")
if not self.test_plan_suffix:
self.test_plan_suffix = self.cfg.get("test_plan_suffix", None)
self.test_plan_suffix = self.cfg.get("test_plan_suffix")
if not self.test_run_key:
self.test_run_key = self.cfg.get("test_run_key", None)
self.test_run_key = self.cfg.get("test_run_key")
if not self.test_run_folder:
self.test_run_folder = self.cfg.get("test_run_folder", None)
self.test_run_folder = self.cfg.get("test_run_folder")
if not self.test_run_suffix:
self.test_run_suffix = self.cfg.get("test_run_suffix", "test run " + datetime.now().strftime("%Y%m%d%H%M"))
self.test_run_suffix = self.cfg.get("test_run_suffix", f"test run {datetime.now().strftime('%Y%m%d%H%M')}")

@pytest.hookimpl(trylast=True)
def pytest_collection_modifyitems(
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
entry_points={"pytest11": ["adaptavist = pytest_adaptavist"]},
platforms="any",
python_requires=">=3.8",
install_requires=["adaptavist>=2.1.0", "pytest>=5.4.0", "pytest-assume>=2.3.2", "pytest-metadata"],
install_requires=["adaptavist>=2.1.0", "pytest>=5.4.0", "pytest-assume>=2.3.2", "pytest-metadata>=1.6.0"],
extras_require={"test": ["beautifulsoup4", "lxml", "requests"]},
setup_requires=["setuptools_scm"],
keywords="python pytest adaptavist kanoah tm4j jira test testmanagement report",
Expand Down
2 changes: 1 addition & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def system_test_preconditions() -> bool:

try:
requests.adapters.DEFAULT_RETRIES = 1
requests.get(atmcfg.global_config.get("jira_server"), timeout=1)
requests.get(atmcfg.global_config.get("jira_server"), timeout=1) # type: ignore
return True
except Exception: # pylint: disable=broad-except
return False
Expand Down
22 changes: 8 additions & 14 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
"""Fixtures for tests."""
from __future__ import annotations

import os
import shutil
from contextlib import suppress
from typing import Generator
from unittest.mock import patch

Expand All @@ -24,22 +22,19 @@ def pytest_configure(config: Config):
@pytest.fixture(scope="session", autouse=True)
def create_test_plan(request: pytest.FixtureRequest) -> str | None:
"""Creates a test plan. All system test will link the test cycle with this test plan."""
if (
system_test_preconditions() and request.config.option.markexpr != "not system"
): # This should only be used if test is a system test
if system_test_preconditions() and request.config.option.markexpr != "not system":
# This should only be used if test is a system test
config = read_global_config()
atm = Adaptavist(config["jira_server"], config["jira_username"], config["jira_password"])
return atm.create_test_plan(config["project_key"], "pytest_adaptavist_system_test")
return None


@pytest.fixture(autouse=True)
def environmental_test_plan(request: pytest.FixtureRequest, create_test_plan: str | None) -> Generator[None, None, None]:
def set_test_plan(monkeypatch: pytest.MonkeyPatch, request: pytest.FixtureRequest, create_test_plan: str | None):
"""Set test plan key for system tests."""
if request.node.get_closest_marker("system"):
os.environ["TEST_PLAN_KEY"] = create_test_plan
yield
with suppress(KeyError):
del os.environ["TEST_PLAN_KEY"]
monkeypatch.setenv("TEST_PLAN_KEY", create_test_plan or "")


@pytest.fixture
Expand All @@ -53,18 +48,17 @@ def adaptavist(pytester: pytest.Pytester) -> Generator[Adaptavist, None, None]:


@pytest.fixture(name="test_run")
def create_test_run(adaptavist: Adaptavist) -> Generator[str, None, None]:
def create_test_run(monkeypatch: pytest.MonkeyPatch, adaptavist: Adaptavist) -> Generator[str, None, None]:
"""Create a new test run."""
config = read_global_config()
if test_run := adaptavist.create_test_run(config["project_key"], "pytest_system_tests"):
os.environ["TEST_RUN_KEY"] = test_run
monkeypatch.setenv("TEST_RUN_KEY", test_run)
yield test_run
del os.environ["TEST_RUN_KEY"]


@pytest.fixture
def configure(pytester: pytest.Pytester):
"""Configure environment for unittests."""
"""Configure environment for unit tests."""
pytester.mkdir("config")
with open("config/global_config.json", "w", encoding="utf8") as file:
file.write('{"jira_server": "https://jira.test", "project_key": "TEST", "test_run_key":"TEST-C1"}')
Expand Down
41 changes: 5 additions & 36 deletions tests/test_atm_configuration.py
Original file line number Diff line number Diff line change
@@ -1,62 +1,31 @@
""" Test Adaptavist test management configuration."""
import os

import pytest
from _pytest.config import ExitCode

from pytest_adaptavist._atm_configuration import ATMConfiguration


def test_get():
def test_get(monkeypatch: pytest.MonkeyPatch):
"""Test atm get function. Config dictionary is preferred over OS environment."""
atm_config = ATMConfiguration()
atm_config.global_config["cfg_test_variable"] = "correct source"

os.environ["cfg_test_variable"] = "wrong source"

monkeypatch.setenv("cfg_test_variable", "wrong source")
assert atm_config.get("cfg_test_variable") == "correct source"

atm_config.global_config["cfg_test"] = "test_cfg"
assert atm_config.get("test") == "test_cfg"


def test_get_environ():
def test_get_environ(monkeypatch: pytest.MonkeyPatch):
"""Test that an OS environment variable is returned if no config is set in dictionary."""
atm_config = ATMConfiguration()

os.environ["test_variable"] = "variable from environment"
monkeypatch.setenv("test_variable", "variable from environment")
assert atm_config.get("test_variable") == "variable from environment"

os.environ["UPPER_TEST_VARIABLE"] = "upper case variable"
monkeypatch.setenv("UPPER_TEST_VARIABLE", "upper case variable")
assert atm_config.get("upper_test_variable") == "upper case variable"


@pytest.mark.usefixtures("adaptavist_mock")
def test_atm_ini(pytester: pytest.Pytester):
"""Test if restrict_user is handles correctly"""
pytester.makepyfile(
"""
def test_T1(meta_block):
with meta_block():
with meta_block(1) as mb_1:
mb_1.check(True)
"""
)
pytester.makeini(
"""
[pytest]
test_run_key = C1
"""
)
report = pytester.inline_run("--adaptavist")
assert report._pluginmanager.get_plugin("_adaptavist").test_run_key == "C1" # pylint: disable=protected-access

os.environ["test_run_key"] = "C2"
report = pytester.inline_run("--adaptavist")
del os.environ["test_run_key"]
assert report._pluginmanager.get_plugin("_adaptavist").test_run_key == "C2" # pylint: disable=protected-access


@pytest.mark.parametrize(
"input_values, output_values",
[
Expand Down
9 changes: 3 additions & 6 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
"""Test CLI behavior."""

import os

import pytest
from _pytest.config import ExitCode

Expand Down Expand Up @@ -31,7 +28,7 @@ def test_adaptavist_enabled(self, pytester: pytest.Pytester):
assert config.getini("adaptavist")

@pytest.mark.usefixtures("adaptavist_mock")
def test_invalid_branch(self, pytester: pytest.Pytester):
def test_invalid_branch(self, pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch):
"""Test the correct behavior of an invalid branch."""
pytester.makepyfile(
"""
Expand All @@ -41,13 +38,13 @@ def test_TEST_T123():
assert True
"""
)
os.environ["GIT_BRANCH"] = "test"
monkeypatch.setenv("GIT_BRANCH", "test")
report = pytester.runpytest("--adaptavist", "--restrict-branch")
assert report.ret == ExitCode.INTERNAL_ERROR
report = pytester.runpytest("--restrict-branch")
assert report.ret == ExitCode.OK
report = pytester.runpytest("--adaptavist", "--restrict-branch", "--restrict-branch-name=test")
assert report.ret == ExitCode.OK
os.environ["GIT_BRANCH"] = "origin/master"
monkeypatch.setenv("GIT_BRANCH", "origin/master")
report = pytester.runpytest("--adaptavist", "--restrict-branch")
assert report.ret == ExitCode.OK
14 changes: 3 additions & 11 deletions tests/test_decorators.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Test decorator usage."""

import pytest
from adaptavist import Adaptavist

Expand Down Expand Up @@ -48,9 +47,7 @@ def test_dummy(self, a):
assert True
"""
)
outcome = pytester.runpytest().parseoutcomes()
assert outcome["skipped"] == 2
assert "passed" not in outcome
pytester.runpytest().assert_outcomes(skipped=2)

@pytest.mark.usefixtures("adaptavist_mock", "configure")
def test_block_decorator_with_class_skipif_decorator(self, pytester: pytest.Pytester):
Expand Down Expand Up @@ -178,9 +175,7 @@ def test_dasda(self):
"""
)
_, etrs, _ = adaptavist_mock
report = pytester.runpytest("--adaptavist")
outcome = report.parseoutcomes()
assert outcome["passed"] == 1
pytester.runpytest("--adaptavist").assert_outcomes(passed=1)
assert etrs.call_count == 1
assert etrs.call_args.kwargs["test_case_key"] == "TEST-T121"

Expand Down Expand Up @@ -216,10 +211,7 @@ def test_dummy(self):
assert True
"""
)
report = pytester.runpytest("--adaptavist")
outcome = report.parseoutcomes()
assert outcome["skipped"] == 1
assert "passed" not in outcome
pytester.runpytest("--adaptavist").assert_outcomes(skipped=1)

def test_project_decorator(self, pytester: pytest.Pytester, adaptavist_mock: AdaptavistMock):
"""Test project decorator."""
Expand Down
1 change: 0 additions & 1 deletion tests/test_helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Test helper functions."""

from adaptavist.const import STATUS_BLOCKED, STATUS_FAIL, STATUS_IN_PROGRESS, STATUS_NOT_EXECUTED, STATUS_PASS
from bs4 import BeautifulSoup

Expand Down
56 changes: 56 additions & 0 deletions tests/test_ini.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""Test pytest.ini configuration."""
import pytest


@pytest.mark.usefixtures("adaptavist_mock")
class TestIniConfig:
"""Test pytest.ini configuration on unit test level."""

@pytest.mark.parametrize(
"option",
[
"project_key",
"test_case_keys",
"test_case_order",
"test_case_range",
"test_environment",
"test_plan_folder",
"test_plan_suffix",
"test_plan_key",
"test_run_folder",
"test_run_key",
"test_run_suffix",
],
)
def test_ini_config_strings(self, pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch, option: str):
"""Test that string values in pytest.ini are correctly used and recognized by pytest."""
monkeypatch.setenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", "1")
pytester.makepyfile(
"""
def test_T1(meta_block):
with meta_block(1) as mb_1:
mb_1.check(True)
"""
)
pytester.makeini(
f"""
[pytest]
{option} = C1
"""
)

report = pytester.inline_run("--adaptavist", plugins=["adaptavist", "assume"])
assert getattr(report._pluginmanager.get_plugin("_adaptavist"), option) in (
"C1",
["C1"],
) # pylint: disable=protected-access

result = pytester.runpytest("--adaptavist", plugins=["adaptavist", "assume"])
assert "warnings" not in result.parseoutcomes()

monkeypatch.setenv(option, "C2")
report = pytester.inline_run("--adaptavist", plugins=["adaptavist", "assume"])
assert getattr(report._pluginmanager.get_plugin("_adaptavist"), option) in (
"C2",
["C2"],
) # pylint: disable=protected-access
1 change: 0 additions & 1 deletion tests/test_init.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
""" Test init file of pytest_adaptavist"""

import pytest


Expand Down
Loading

0 comments on commit 85bb70a

Please sign in to comment.