testing/mozbase/mozdevice/sut_tests/runtests.py

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:83170370a15d
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5 from optparse import OptionParser
6 import os
7 import re
8 import sys
9 import unittest
10
11 import mozlog
12
13 import dmunit
14 import genfiles
15
16
17 def main(ip, port, heartbeat_port, scripts, directory, isTestDevice, verbose):
18 dmunit.ip = ip
19 dmunit.port = port
20 dmunit.heartbeat_port = heartbeat_port
21 if verbose:
22 dmunit.log_level = mozlog.DEBUG
23
24 suite = unittest.TestSuite()
25
26 genfiles.gen_test_files()
27
28 if scripts:
29 # Ensure the user didn't include the .py on the name of the test file
30 # (and get rid of it if they did)
31 scripts = map(lambda x: x.split('.')[0], scripts)
32 else:
33 # Go through the directory and pick up everything
34 # named test_*.py and run it
35 testfile = re.compile('^test_.*\.py$')
36 files = os.listdir(directory)
37
38 for f in files:
39 if testfile.match(f):
40 scripts.append(f.split('.')[0])
41
42 testLoader = dmunit.DeviceManagerTestLoader(isTestDevice)
43 for s in scripts:
44 suite.addTest(testLoader.loadTestsFromModuleName(s))
45 unittest.TextTestRunner(verbosity=2).run(suite)
46
47 genfiles.clean_test_files()
48
49
50 if __name__ == "__main__":
51
52 default_ip = '127.0.0.1'
53 default_port = 20701
54
55 env_ip, _, env_port = os.getenv('TEST_DEVICE', '').partition(':')
56 if env_port:
57 try:
58 env_port = int(env_port)
59 except ValueError:
60 print >> sys.stderr, "Port in TEST_DEVICE should be an integer."
61 sys.exit(1)
62
63 # Deal with the options
64 parser = OptionParser()
65 parser.add_option("--ip", action="store", type="string", dest="ip",
66 help="IP address for device running SUTAgent, defaults "
67 "to what's provided in $TEST_DEVICE or 127.0.0.1",
68 default=(env_ip or default_ip))
69
70 parser.add_option("--port", action="store", type="int", dest="port",
71 help="Port of SUTAgent on device, defaults to "
72 "what's provided in $TEST_DEVICE or 20701",
73 default=(env_port or default_port))
74
75 parser.add_option("--heartbeat", action="store", type="int",
76 dest="heartbeat_port", help="Port for heartbeat/data "
77 "channel, defaults to 20700", default=20700)
78
79 parser.add_option("--script", action="append", type="string",
80 dest="scripts", help="Name of test script to run, "
81 "can be specified multiple times", default=[])
82
83 parser.add_option("--directory", action="store", type="string", dest="dir",
84 help="Directory to look for tests in, defaults to "
85 "current directory", default=os.getcwd())
86
87 parser.add_option("--testDevice", action="store_true", dest="isTestDevice",
88 help="Specifies that the device is a local test agent",
89 default=False)
90
91 parser.add_option("-v", "--verbose", action="store_true", dest="verbose",
92 help="Verbose DeviceManager output", default=False)
93
94 (options, args) = parser.parse_args()
95
96 main(options.ip, options.port, options.heartbeat_port, options.scripts,
97 options.dir, options.isTestDevice, options.verbose)

mercurial