蓝色天空网 · mbsky.com
测试框架:生命周期、断言和报告
框架通过必须来自实际浏览器结果。
固定本地测试页的实际浏览器结果,不是在线Python解释器。下载代码后可在独立环境修改输入并重新运行。
本章Python入口 · 完整实验实现 · 本站测试服务 · 本站页面交互

当前输入实际检查
| 实际检查 | 结果 |
|---|---|
| fixture-initial-no-severe-errors | 通过 |
| unittest-exit | 通过 |
| unittest-2actual-tests | 通过 |
| pytest-exit | 通过 |
| pytest-actual-count | 通过 |
| pytest-no-fail-errors | 通过 |
| HTML-real-report | 通过 |
| four-independent-test-cleanups | 通过 |
| fixture-sessions-closed-pytest-test_css-aa555843 | 通过 |
| one-browser-per-test-pytest-test_css-aa555843 | 通过 |
| fixture-sessions-closed-pytest-test_missing-c4f683b2 | 通过 |
| one-browser-per-test-pytest-test_missing-c4f683b2 | 通过 |
| fixture-sessions-closed-unittest-33128e9d | 通过 |
| one-browser-per-test-unittest-33128e9d | 通过 |
| fixture-sessions-closed-unittest-b2ca5d43 | 通过 |
| one-browser-per-test-unittest-b2ca5d43 | 通过 |
| framework-search-outcome | 通过 |
| screenshot-saved-actual-browser | 通过 |
| PNG-magic-actual-browser | 通过 |
| PNG-extent-actual-browser | 通过 |
| all-created-sessions-quit | 通过 |
本章完整入口
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')