view tests/__init__.py @ 72:e8b3b336e63e draft default tip

Update version.
author David Barts <n5jrn@me.com>
date Mon, 15 Jul 2019 13:17:48 -0700
parents 88adf10be709
children
line wrap: on
line source

#!/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__)