:param server_func: 启动PyWebIO服务器的函数 :param test_func: 测试的函数。人工测试时不会被运行 (server_proc, browser) :param port: 启动的PyWebIO服务器的端口
(server_func, test_func, address='http://localhost:8080?_pywebio_debug=1', chrome_options=None)
| 26 | |
| 27 | |
| 28 | def run_test(server_func, test_func, address='http://localhost:8080?_pywebio_debug=1', chrome_options=None): |
| 29 | """ |
| 30 | :param server_func: 启动PyWebIO服务器的函数 |
| 31 | :param test_func: 测试的函数。人工测试时不会被运行 (server_proc, browser) |
| 32 | :param port: 启动的PyWebIO服务器的端口 |
| 33 | """ |
| 34 | if len(sys.argv) not in (1, 2) or (len(sys.argv) == 2 and sys.argv[-1] not in ('server', 'auto', 'debug')): |
| 35 | print(USAGE.format(name=sys.argv[0])) |
| 36 | return |
| 37 | |
| 38 | if len(sys.argv) != 2: # when execute test script with no argument, only start server |
| 39 | try: |
| 40 | server_func() |
| 41 | except KeyboardInterrupt: |
| 42 | pass |
| 43 | sys.exit() |
| 44 | |
| 45 | if chrome_options is None: |
| 46 | chrome_options = default_chrome_options |
| 47 | |
| 48 | if sys.argv[-1] == 'auto': |
| 49 | default_chrome_options.add_argument('--headless') |
| 50 | proc = subprocess.Popen(['coverage', 'run', '--source', 'pywebio', '--append', |
| 51 | sys.argv[0]], stdout=sys.stdout, stderr=subprocess.STDOUT, text=True) |
| 52 | elif sys.argv[-1] == 'debug': |
| 53 | # start server as sub process |
| 54 | proc = subprocess.Popen(['python3', sys.argv[0]], stdout=sys.stdout, stderr=subprocess.STDOUT, text=True) |
| 55 | |
| 56 | browser = None |
| 57 | try: |
| 58 | browser = webdriver.Chrome(options=chrome_options) |
| 59 | browser.set_window_size(1000, 900) |
| 60 | port_str = urlparse(address).netloc.split(':', 1)[-1] or '80' |
| 61 | asyncio.run(wait_host_port('localhost', int(port_str))) |
| 62 | browser.get(address) |
| 63 | browser.implicitly_wait(10) |
| 64 | test_func(proc, browser) |
| 65 | finally: |
| 66 | if browser: |
| 67 | if sys.argv[-1] == 'debug': |
| 68 | input('press ENTER to exit') |
| 69 | |
| 70 | browser.quit() |
| 71 | |
| 72 | # 不要使用 proc.terminate() ,因为coverage会无法保存分析数据 |
| 73 | proc.send_signal(signal.SIGINT) |
| 74 | print("Closed browser and PyWebIO server") |
nothing calls this directly
no test coverage detected
searching dependent graphs…