71
|
1 #!/usr/bin/env python3
|
|
2
|
|
3 # I m p o r t s
|
|
4
|
|
5 import unittest
|
|
6 import tincan
|
|
7 import logging
|
|
8 import subprocess
|
|
9 import time
|
|
10
|
|
11 # V a r i a b l e s
|
|
12
|
|
13 LOGGERNAME = "run-tests-server"
|
|
14 LOGNAME = "run-tests-server.log"
|
|
15
|
|
16 # F u n c t i o n s
|
|
17
|
|
18 def _flush(logger):
|
|
19 for handler in logger.handlers:
|
|
20 handler.flush()
|
|
21
|
|
22 # C l a s s e s
|
|
23
|
|
24 class ServerFixture(unittest.TestCase):
|
|
25 """
|
|
26 This tests if we can launch something successfully or not. It is intended
|
|
27 to have but a single test case, which checks that cls.errors is 0.
|
|
28 """
|
|
29 files = None
|
|
30 port = None
|
|
31 app = None
|
|
32 errors = None
|
|
33
|
|
34 @classmethod
|
|
35 def setUpClass(cls):
|
|
36 mylog = logging.getLogger(LOGGERNAME)
|
|
37 mylog.info("*** BEGIN %s.%s", cls.__module__, cls.__name__)
|
|
38 cls.app, cls.errors = tincan.launch(fsroot=cls.files, logger=mylog)
|
|
39 mylog.info("*** END %s.%s", cls.__module__, cls.__name__)
|
|
40
|
|
41 @classmethod
|
|
42 def tearDownClass(cls):
|
|
43 pass
|
|
44
|
|
45 class RoutesFixture(unittest.TestCase):
|
|
46 """
|
|
47 This is for testing routes. It assumes the server will start and run,
|
|
48 and runs that server in a separate process. It can (and often will) have
|
|
49 multiple test cases, which will use requests and html.parser to verify
|
|
50 the output is as expected.
|
|
51 """
|
|
52 files = None
|
|
53 port = None
|
|
54 server = None
|
|
55 logger = None
|
|
56
|
|
57 @classmethod
|
|
58 def setUpClass(cls):
|
|
59 mylog = logging.getLogger(LOGGERNAME)
|
|
60 mylog.info("*** BEGIN %s.%s", cls.__module__, cls.__name__)
|
|
61 _flush(mylog)
|
|
62 cls.logger = open(LOGNAME, "a")
|
|
63 cls.server = subprocess.Popen(
|
|
64 [ "bin/launch", "--port={0}".format(cls.port), cls.files, ],
|
|
65 stdin=subprocess.DEVNULL, stdout=cls.logger, stderr=cls.logger)
|
|
66 time.sleep(3) # xxx
|
|
67
|
|
68 @classmethod
|
|
69 def tearDownClass(cls):
|
|
70 cls.server.terminate()
|
|
71 cls.server.wait()
|
|
72 cls.logger.close()
|
|
73 mylog = logging.getLogger(LOGGERNAME)
|
|
74 mylog.info("*** END %s.%s", cls.__module__, cls.__name__)
|