From 620ab0ac9b55ae7c274139664a0cd1b9ecc1cdbc Mon Sep 17 00:00:00 2001
From: "Mads M. Pedersen" <mmpe@dtu.dk>
Date: Fri, 25 May 2018 12:54:23 +0200
Subject: [PATCH] changed test_try_me to pytest

---
 tests/test_try_me.py | 51 ++++++++++++++++++--------------------------
 topfarm/_topfarm.py  |  1 -
 2 files changed, 21 insertions(+), 31 deletions(-)

diff --git a/tests/test_try_me.py b/tests/test_try_me.py
index 47a059e4..5ede5239 100644
--- a/tests/test_try_me.py
+++ b/tests/test_try_me.py
@@ -1,38 +1,29 @@
-'''
-Created on 17. maj 2018
-
-@author: mmpe
-'''
-import unittest
-import topfarm
-import pkgutil
 import importlib
-import inspect
-import ast
+import os
+import pkgutil
 import warnings
-from topfarm.cost_models.fuga import lib_reader
 import mock
 import pytest
-import os
-
+import topfarm
 
-class TestTryMe(unittest.TestCase):
 
-    
-    def test_try_me(self):
-        if os.name == 'posix' and "DISPLAY" not in os.environ:
-            pytest.xfail("No display")
-        package = topfarm
-        for _, modname, _ in pkgutil.walk_packages(package.__path__, package.__name__ + '.'):
-            with warnings.catch_warnings():
-                warnings.simplefilter("ignore")
-                m = importlib.import_module(modname)
-            if 'try_me' in dir(m):
-                print("Checking %s.try_me" % modname)
-                with mock.patch.object(m, "__name__", "__main__"):
-                    getattr(m, 'try_me')()
+def get_try_me_modules():
+    package = topfarm
+    modules = []
+    for _, modname, _ in pkgutil.walk_packages(package.__path__, package.__name__ + '.'):
+        with warnings.catch_warnings():
+            warnings.simplefilter("ignore")
+            m = importlib.import_module(modname)
+        if 'try_me' in dir(m):
+            modules.append(m)
+    return modules
 
 
-if __name__ == "__main__":
-    #import sys;sys.argv = ['', 'Test.testName']
-    unittest.main()
+@pytest.mark.parametrize("module", get_try_me_modules())
+def test_try_me(module):
+    # check that all try_me module examples run without errors 
+    if os.name == 'posix' and "DISPLAY" not in os.environ:
+        pytest.xfail("No display")
+    print("Checking %s.try_me" % module.__name__)
+    with mock.patch.object(module, "__name__", "__main__"):
+        getattr(module, 'try_me')()
diff --git a/topfarm/_topfarm.py b/topfarm/_topfarm.py
index ed17bb88..ae4ecc8a 100644
--- a/topfarm/_topfarm.py
+++ b/topfarm/_topfarm.py
@@ -1,7 +1,6 @@
 import time
 import numpy as np
 import warnings
-from wetb.utils.timing import print_time
 with warnings.catch_warnings():
     warnings.simplefilter('ignore', FutureWarning)
     from openmdao.api import Problem, ScipyOptimizeDriver, IndepVarComp
-- 
GitLab