web-automation-pytest-demo/main-runtest.py

89 lines
2.7 KiB
Python

from os import system as exec_system_command
from os.path import exists as is_file_exists
from os import environ
from os.path import dirname
from sys import argv as term_args
from sys import executable
import platform
import json
# Must run as the main program.
if __name__ != '__main__':
raise SystemExit('The current file can only be run as the main program, and it is forbidden to be imported.')
# Main command line.
first_command_status = exec_system_command("""
pytest --alluredir ./allure-results --clean-alluredir %s
""" % "\x20".join(term_args[1:]))
allure_results = '%s/allure-results' % dirname(__file__)
if is_file_exists(allure_results):
allure_results_categories = '%s/categories.json' % allure_results
open(allure_results_categories, mode='w', encoding='utf-8').write(json.dumps([
{
"name": "通过",
"matchedStatuses": [
"passed"
]
},
{
"name": "失败",
"matchedStatuses": [
"failed"
]
},
{
"name": "测试发生错误",
"matchedStatuses": [
"broken"
]
},
{
"name": "加载超时异常",
"matchedStatuses": [
"broken"
],
"messageRegex": ".*TimeoutException.*"
}
], indent=4, ensure_ascii=False))
if is_file_exists(allure_results):
allure_results_environment_properties = '%s/environment.properties' % allure_results
open(allure_results_environment_properties, mode='w').write("OS = %s\nPython = %s\nExecute = %s" % (
('%s (%s %s) %s' % (platform.node(), platform.system(), platform.release(), platform.machine().lower())),
('%s (%s)' % (platform.python_version(), platform.architecture()[0])),
executable.replace("\\", '/')
))
if is_file_exists(allure_results):
executor_file = '%s/executor.json' % allure_results
if True:
c = 0
if 'PYCHARM_HOSTED' in environ.keys():
c = 1
if 'CI' in environ.keys() or 'JENKINS_HOME' in environ.keys():
c = 2
match c:
case 0:
open(executor_file, mode='w').write(json.dumps(
{
'name': 'Python',
'type': 'python',
'buildName': ' '
},
indent=4, ensure_ascii=False
))
case 1:
open(executor_file, mode='w').write(json.dumps(
{
'name': 'PyCharm',
'type': 'pycharm',
'buildName': ' '
},
indent=4, ensure_ascii=False
))
# Return to the main command line status code.
exit(first_command_status)