root/galaxy-central/eggs/twill-0.9-py2.6.egg/twill/unit.py

リビジョン 3, 2.0 KB (コミッタ: kohda, 14 年 前)

Install Unix tools  http://hannonlab.cshl.edu/galaxy_unix_tools/galaxy.html

行番号 
1"""
2Support functionality for using twill in unit tests.
3"""
4
5import sys, os, time
6from cStringIO import StringIO
7
8# package import
9from parse import execute_file
10
11class TestInfo:
12    """
13    Object containing info for a test: script to run, server function to
14    run, and port to run it on.  Note that information about server port
15    *must* be decided by the end of the __init__ function.
16
17    The optional sleep argument specifies how many seconds to wait for the
18    server to set itself up.  Default is 0.
19    """
20   
21    def __init__(self, script, server_fn, port, sleep=0):
22        self.script = script
23        self.server_fn = server_fn
24        self.port = port
25        self.stdout = None
26        self.stderr = None
27        self.sleep = sleep
28
29    def start_server(self):
30        # save old stdout/stderr
31        old_out, old_err = sys.stdout, sys.stderr
32
33        # create new stdout/stderr
34        self.stdout = sys.stdout = StringIO()
35        self.stderr = sys.stderr = StringIO()
36       
37        try:
38            self.server_fn()
39        finally:
40            # restore stdout/stderr
41            sys.stdout, sys.stderr = old_out, old_err
42
43    def run_script(self):
44        """
45        Run the given twill script on the given server.
46        """
47        time.sleep(self.sleep)
48        url = self.get_url()
49        execute_file(self.script, initial_url=url)
50
51    def get_url(self):
52        "Calculate the test server URL."
53        return "http://localhost:%d/" % (self.port,)
54
55#
56# run_test
57#
58
59def run_test(test_info):
60    """
61    Run a test on a Web site where the Web site is running in a child
62    process.
63    """
64    pid = os.fork()
65
66    if pid is 0:
67        run_child_process(test_info)
68        # never returns...
69
70    #
71    # run twill test script.
72    #
73   
74    child_pid = pid
75    try:
76        test_info.run_script()
77    finally:
78        os.kill(child_pid, 9)
79
80#
81# run_child_process
82#
83       
84def run_child_process(test_info):
85    """
86    Run a Web server in a child process.
87    """
88    test_info.start_server()
89    sys.exit(0)
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。