from selenium_lab import *
@case('selenium-unittest')
def framework_case(l,d,b,s):
import xml.etree.ElementTree as ET
root=Path(__file__).resolve().parent;output=l.out/'framework';output.mkdir();env=os.environ.copy();env.update(MBSKY_TEST_OUTPUT=str(output),PYTHONDONTWRITEBYTECODE='1');unit=subprocess.run([sys.executable,'-E','-s','-B','-X','utf8','-m','unittest','-v','test_unittest_flow.py'],cwd=root,env=env,capture_output=True,text=True,encoding='utf8',timeout=50,creationflags=getattr(subprocess,'CREATE_NO_WINDOW',0));(output/'unittest.txt').write_text(unit.stdout+unit.stderr,encoding='utf8');l.eq('unittest-exit',unit.returncode,0);l.check('unittest-2actual-tests','Ran 2 tests'in unit.stderr and'OK'in unit.stderr);config=output/'pytest.ini';config.write_text('[pytest]\n',encoding='utf8');report=output/'report.html';junit=output/'pytest.xml';test=subprocess.run([sys.executable,'-E','-s','-B','-X','utf8','-m','pytest',str(root/'test_pytest_flow.py'),'-q','-n','2','-p','no:cacheprovider','-c',str(config),'--basetemp',str(output/'pytest-temp'),'--html',str(report),'--self-contained-html','--junitxml',str(junit)],cwd=output,env=env,capture_output=True,text=True,encoding='utf8',timeout=70,creationflags=getattr(subprocess,'CREATE_NO_WINDOW',0));(output/'pytest.txt').write_text(test.stdout+test.stderr,encoding='utf8');l.eq('pytest-exit',test.returncode,0);suites=ET.parse(junit).getroot().findall('testsuite');l.eq('pytest-actual-count',sum(int(v.attrib['tests'])for v in suites),2);l.eq('pytest-no-fail-errors',sum(int(v.attrib.get('failures','0'))+int(v.attrib.get('errors','0'))for v in suites),0);l.check('HTML-real-report',report.is_file()and report.stat().st_size>1000);receipts=list(output.glob('*/cleanup.json'));l.eq('four-independent-test-cleanups',len(receipts),4)
for receipt in receipts:
value=json.loads(receipt.read_text(encoding='utf8'));l.eq('fixture-sessions-closed-'+receipt.parent.name,value['opened'],value['closed']);l.eq('one-browser-per-test-'+receipt.parent.name,len(value['opened']),1)
l.metadata['framework']={'unittest_tests':2,'pytest_tests':2,'pytest_workers':2,'warnings_and_full_report':'retained in current private output, not publicly exported'};l.eq('framework-search-outcome',search(d,'JavaScript'),['JavaScript事件']);l.pending_case('framework-failure-init-and-CI','Actual two unittest and two pytest/xdist successes plus cleanup receipts; intentional failed setup/retry/CI runners/other language frameworks not executed')
if __name__ == "__main__":
run('selenium-unittest')
本例需在 Windows/Python3.12/Selenium4.52.0/Edge及驱动156.0.4314.58;GridServer4.52/Temurin21.0.13;独立无头会话 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows/Python3.12/Selenium4.52.0/Edge及驱动156.0.4314.58;GridServer4.52/Temurin21.0.13;独立无头会话
入口文件:projects/selenium-lab/selenium-unittest.py
预期结果与验证条件观察提示
- 本章固定输入断言通过,错误/等待/业务结果和退出分别验证
- 截图对应本站当前测试页;未执行平台或能力不因本轮通过自动升级
请结合实际环境逐项观察。
附件与关联工程包下载
课程关联工程包(登记关联15章) · 含当前章节或实例 · mbskycom202611a531.zip