From 59f35810e65c6a06adce23c54dd02bc9332a08c0 Mon Sep 17 00:00:00 2001 From: Adam Israel Date: Fri, 24 Feb 2017 10:47:15 +0100 Subject: [PATCH] pytest example An example of using pytest, driven with config options defined in a yaml Signed-off-by: Adam Israel --- test/example/config.yaml | 8 ++++++++ test/example/conftest.py | 22 ++++++++++++++++++++++ test/example/test_strings.py | 9 +++++++++ 3 files changed, 39 insertions(+) create mode 100644 test/example/config.yaml create mode 100644 test/example/conftest.py create mode 100644 test/example/test_strings.py diff --git a/test/example/config.yaml b/test/example/config.yaml new file mode 100644 index 00000000..b6b61767 --- /dev/null +++ b/test/example/config.yaml @@ -0,0 +1,8 @@ +so_host: + type: 'string' + default: '127.0.0.1' + description: 'Host name or ip of the SO' +so_port: + type: int + default: 80 + description: 'Port' diff --git a/test/example/conftest.py b/test/example/conftest.py new file mode 100644 index 00000000..3798a150 --- /dev/null +++ b/test/example/conftest.py @@ -0,0 +1,22 @@ +import yaml + + +config = None +with open('config.yaml') as f: + config = yaml.load(f) + + +def pytest_addoption(parser): + for param in config: + parser.addoption("--{}".format(param), + action="store", + type="{}".format(config[param]["type"]), + default="{}".format(config[param]["default"]), + help="{}".format(config[param]["description"]) + ) + + +def pytest_generate_tests(metafunc): + for param in config: + if param in metafunc.fixturenames: + metafunc.parametrize(param, [metafunc.config.getoption(param)]) diff --git a/test/example/test_strings.py b/test/example/test_strings.py new file mode 100644 index 00000000..c156dc95 --- /dev/null +++ b/test/example/test_strings.py @@ -0,0 +1,9 @@ + + +def test_host(so_host): + assert so_host == '127.0.0.1' + + +def test_port(so_host, so_port): + assert so_host == '127.0.0.1' + assert so_port == 80 -- 2.25.1