pytest example 85/1185/2
authorAdam Israel <adam.israel@canonical.com>
Fri, 24 Feb 2017 09:47:15 +0000 (10:47 +0100)
committergarciadeblas <gerardo.garciadeblas@telefonica.com>
Fri, 24 Feb 2017 09:57:10 +0000 (10:57 +0100)
An example of using pytest, driven with config options defined in a yaml

Signed-off-by: Adam Israel <adam.israel@canonical.com>
test/example/config.yaml [new file with mode: 0644]
test/example/conftest.py [new file with mode: 0644]
test/example/test_strings.py [new file with mode: 0644]

diff --git a/test/example/config.yaml b/test/example/config.yaml
new file mode 100644 (file)
index 0000000..b6b6176
--- /dev/null
@@ -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 (file)
index 0000000..3798a15
--- /dev/null
@@ -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 (file)
index 0000000..c156dc9
--- /dev/null
@@ -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