diff tests/__init__.py @ 71:88adf10be709 draft

Add tests.
author David Barts <n5jrn@me.com>
date Mon, 15 Jul 2019 13:16:31 -0700
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/__init__.py	Mon Jul 15 13:16:31 2019 -0700
@@ -0,0 +1,74 @@
+#!/usr/bin/env python3
+
+# I m p o r t s
+
+import unittest
+import tincan
+import logging
+import subprocess
+import time
+
+# V a r i a b l e s
+
+LOGGERNAME = "run-tests-server"
+LOGNAME = "run-tests-server.log"
+
+# F u n c t i o n s
+
+def _flush(logger):
+    for handler in logger.handlers:
+        handler.flush()
+
+# C l a s s e s
+
+class ServerFixture(unittest.TestCase):
+    """
+    This tests if we can launch something successfully or not. It is intended
+    to have but a single test case, which checks that cls.errors is 0.
+    """
+    files = None
+    port = None
+    app = None
+    errors = None
+
+    @classmethod
+    def setUpClass(cls):
+        mylog = logging.getLogger(LOGGERNAME)
+        mylog.info("*** BEGIN %s.%s", cls.__module__, cls.__name__)
+        cls.app, cls.errors = tincan.launch(fsroot=cls.files, logger=mylog)
+        mylog.info("*** END %s.%s", cls.__module__, cls.__name__)
+
+    @classmethod
+    def tearDownClass(cls):
+        pass
+
+class RoutesFixture(unittest.TestCase):
+    """
+    This is for testing routes. It assumes the server will start and run,
+    and runs that server in a separate process. It can (and often will) have
+    multiple test cases, which will use requests and html.parser to verify
+    the output is as expected.
+    """
+    files = None
+    port = None
+    server = None
+    logger = None
+
+    @classmethod
+    def setUpClass(cls):
+        mylog = logging.getLogger(LOGGERNAME)
+        mylog.info("*** BEGIN %s.%s", cls.__module__, cls.__name__)
+        _flush(mylog)
+        cls.logger = open(LOGNAME, "a")
+        cls.server = subprocess.Popen(
+            [ "bin/launch", "--port={0}".format(cls.port), cls.files, ],
+            stdin=subprocess.DEVNULL, stdout=cls.logger, stderr=cls.logger)
+        time.sleep(3)  # xxx
+
+    @classmethod
+    def tearDownClass(cls):
+        cls.server.terminate()
+        cls.server.wait()
+        cls.logger.close()
+        mylog = logging.getLogger(LOGGERNAME)
+        mylog.info("*** END %s.%s", cls.__module__, cls.__name__)