1+ import json
2+ import logging
3+
4+
15def pytest_addoption (parser ):
26 parser .addoption (
37 "--ref" ,
@@ -15,6 +19,14 @@ def pytest_addoption(parser):
1519 choices = ["normal" , "quick" ],
1620 help = "run tests on normal or quick mode" ,
1721 )
22+ parser .addoption (
23+ "--record" ,
24+ action = "store" ,
25+ default = "none" ,
26+ required = False ,
27+ choices = ["none" , "log" ],
28+ help = "tests function param recorded in log files or not" ,
29+ )
1830
1931
2032def pytest_configure (config ):
@@ -23,3 +35,60 @@ def pytest_configure(config):
2335
2436 global QUICK_MODE
2537 QUICK_MODE = config .getoption ("--mode" ) == "quick"
38+
39+ global RECORD_LOG
40+ RECORD_LOG = config .getoption ("--record" ) == "log"
41+ if RECORD_LOG :
42+ global RUNTEST_INFO , BUILTIN_MARKS , REGISTERED_MARKERS
43+ RUNTEST_INFO = {}
44+ BUILTIN_MARKS = {
45+ "parametrize" ,
46+ "skip" ,
47+ "skipif" ,
48+ "xfail" ,
49+ "usefixtures" ,
50+ "filterwarnings" ,
51+ "timeout" ,
52+ "tryfirst" ,
53+ "trylast" ,
54+ }
55+ REGISTERED_MARKERS = {
56+ marker .split (":" )[0 ].strip () for marker in config .getini ("markers" )
57+ }
58+ cmd_args = [
59+ arg .replace (".py" , "" ).replace ("=" , "_" ).replace ("/" , "_" )
60+ for arg in config .invocation_params .args
61+ ]
62+ logging .basicConfig (
63+ filename = "result_{}.log" .format ("_" .join (cmd_args )).replace ("_-" , "-" ),
64+ filemode = "w" ,
65+ level = logging .INFO ,
66+ format = "[%(levelname)s] %(message)s" ,
67+ )
68+
69+
70+ def pytest_runtest_teardown (item , nextitem ):
71+ if not RECORD_LOG :
72+ return
73+ if hasattr (item , "callspec" ):
74+ all_marks = list (item .iter_markers ())
75+ op_marks = [
76+ mark .name
77+ for mark in all_marks
78+ if mark .name not in BUILTIN_MARKS and mark .name not in REGISTERED_MARKERS
79+ ]
80+ if len (op_marks ) > 0 :
81+ params = str (item .callspec .params )
82+ for op_mark in op_marks :
83+ if op_mark not in RUNTEST_INFO :
84+ RUNTEST_INFO [op_mark ] = [params ]
85+ else :
86+ RUNTEST_INFO [op_mark ].append (params )
87+ else :
88+ func_name = item .function .__name__
89+ logging .warning ("There is no mark at {}" .format (func_name ))
90+
91+
92+ def pytest_sessionfinish (session , exitstatus ):
93+ if RECORD_LOG :
94+ logging .info (json .dumps (RUNTEST_INFO , indent = 2 ))
0 commit comments