From: Adam Israel Date: Fri, 24 Feb 2017 09:47:15 +0000 (+0100) Subject: pytest example X-Git-Tag: v1.1.0~3 X-Git-Url: https://osm.etsi.org/gitweb/?p=osm%2Fdevops.git;a=commitdiff_plain;h=59f35810e65c6a06adce23c54dd02bc9332a08c0 pytest example An example of using pytest, driven with config options defined in a yaml Signed-off-by: Adam Israel --- 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