Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicoretti committed Jun 18, 2024
1 parent 9e3c705 commit f5c9b0f
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 13 deletions.
31 changes: 18 additions & 13 deletions pytest-plugin-settings/exasol/pytest_plugin_settings/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import pytest
from functools import singledispatch
from collections.abc import IterableABC as IterableAbc
from collections.abc import Iterable as IterableAbc
from collections.abc import MappingView
from collections import ChainMap
from dataclasses import dataclass
Expand Down Expand Up @@ -74,25 +74,29 @@ class Setting(Generic[T]):
default: Optional[T] = None
help_text: str = ""

@property
def _name(self) -> Name:
return Name(f"{self.prefix}-{self.name}")

@property
def normalized_name(self) -> str:
return str(Name(f"{self.prefix}-{self.name}"))
return str(self._name)

@property
def env(self) -> str:
return self.normalized_name.env()
return self._name.env

@property
def cli(self) -> str:
return self.normalized_name.cli()
return self._name.cli

@property
def ini(self) -> str:
return self.normalized_name.ini()
return self._name.ini

@property
def pytest(self) -> str:
return self.normalized_name.pytest()
return self._name.pytest

@property
def help(self) -> str:
Expand Down Expand Up @@ -177,7 +181,7 @@ def update(self, mapping, category):
def _update_cli(self, cli_arguments):
cli = {
setting.normalized_name: getattr(cli_arguments, setting.pytest)
for setting in self.options
for setting in self._settings
if hasattr(cli_arguments, setting.pytest)
and getattr(cli_arguments, setting.pytest) is not None
}
Expand Down Expand Up @@ -205,32 +209,33 @@ def __init__(self, settings: Iterable[Setting], config: pytest.Config):
cli = from_pytest(settings, config)
cfg = from_pytest_ini(settings, config)
env = os.environ
super().__init__(cli_arguments=cli, environment=env, config=cfg)
super().__init__(settings, cli_arguments=cli, environment=env, config=cfg)


def from_pytest(settings: Iterable[Setting], config: pytest.Config) -> Dict[str, Any]:
pass
return {}


def from_env(settings: Iterable[Setting], env: Dict[str, str]) -> Dict[str, Any]:
pass
return {}


def from_pytest_ini(
settings: Iterable[Setting], config: pytest.Config
) -> Dict[str, Any]:
pass
return {}


@singledispatch
def add_to_pytest_settings(obj, parser):
raise TypeError("Unsupported type")
raise TypeError(f"Unsupported type: {obj.__class__.__name__}, Object name: {obj.__name__}")


@add_to_pytest_settings.register
def _(setting: Setting, parser):
parser.addoption(setting.cli, help=setting.help)
parser.addini(name=setting.ini, help=setting.help)
p = parser if hasattr(parser, "addini") else parser.parser
p.addini(name=setting.ini, help=setting.help)


@add_to_pytest_settings.register
Expand Down
90 changes: 90 additions & 0 deletions pytest-plugin-settings/test/integration/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import pytest
from inspect import cleandoc

pytest_plugins = "pytester"


@pytest.fixture()
def conftest(pytester):
file = pytester.path.joinpath("conftest.py")
file.write_text(
cleandoc(
"""
import pytest
from exasol.pytest_plugin_settings import Group, Setting, PytestResolver, add_to_pytest_settings
PORT = Setting(prefix='', name="port", type=int, default=9999, help_text="Port to connect to")
ADDRESS = Setting(prefix='', name="ip", type=str, default="127.0.0.1", help_text="IP to connect to")
CREDENTIALS = Group(name='credentials', settings=[
Setting(prefix=None, name="user", type=str, default="johndoe", help_text="Username"),
Setting(prefix=None, name="pw", type=str, default="password", help_text="Password")
])
SETTINGS = [
Setting(prefix=None, name="db-name", type=str, default="test.db", help_text="Name of the database"),
Setting(prefix=None, name="db-schema", type=str, default="test_schema", help_text="name of the schema"),
]
def pytest_addoption(parser):
# add a single settings to pytest
add_to_pytest_settings(PORT, parser)
add_to_pytest_settings(ADDRESS, parser)
# add a group of settings to pytest
add_to_pytest_settings(CREDENTIALS, parser)
# add a list of settings to pytest
add_to_pytest_settings(SETTINGS, parser)
def pytest_configure(config):
all_settings = [ADDRESS, PORT] + [ s for s in CREDENTIALS.settings ] + SETTINGS
settings = PytestResolver(all_settings, config)
config.stash["my-plugin-settings"] = settings
@pytest.fixture()
def my_settings(request):
settings = request.config.stash["my-plugin-settings"]
yield settings
@pytest.fixture()
def port(my_settings):
yield my_settings["port"]
@pytest.fixture()
def ip(my_settings):
yield my_settings["ip"]
@pytest.fixture()
def user(my_settings):
yield my_settings["credentials_user"]
@pytest.fixture()
def password(my_settings):
yield my_settings["credentials_pw"]
"""
)
)
file = pytester.path.joinpath("test_smoke.py")
file.write_text(
cleandoc(
"""
def test_settings(user, port):
print()
print(f"username: {user}")
print(f"port: {port}")
def test_smoke_fail():
assert False
def test_smoke_succeed():
assert True
"""
)
)


def test_basic_pytester(pytester, conftest):
result = pytester.runpytest("-s", "-vvv", ".")
#config = pytester.parseconfig()
#configure = pytester.parseconfigure()
result2 = pytester.runpytest("--help")
print(result)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from pytest import Pytester


pytest_plugins = "pytester"

0 comments on commit f5c9b0f

Please sign in to comment.