@@ -21,9 +21,15 @@ def get_raw_data(data_source, size):
2121def process_data (raw_data , algorithm ):
2222 """处理数据生成器,依赖于raw_data"""
2323 if algorithm == "algo1" :
24- return [{"id" : item ["id" ], "processed" : item ["id" ] * 2 } for item in raw_data ]
24+ return [
25+ {"id" : item ["id" ], "processed" : item ["id" ] * 2 }
26+ for item in raw_data
27+ ]
2528 else :
26- return [{"id" : item ["id" ], "processed" : item ["id" ] * 3 } for item in raw_data ]
29+ return [
30+ {"id" : item ["id" ], "processed" : item ["id" ] * 3 }
31+ for item in raw_data
32+ ]
2733
2834
2935@param_generator
@@ -43,18 +49,20 @@ def validate_results(processed_data, threshold):
4349@pytest .mark .parametrize ("algorithm" , ["algo1" , "algo2" ])
4450@pytest .mark .parametrize ("threshold" , [0.5 , 8.0 ])
4551def test_dynamic_params_nesting (
46- data_source , size , algorithm , threshold ,
47- raw_data , processed_data , is_valid
52+ data_source , size , algorithm , threshold , raw_data , processed_data , is_valid
4853):
4954 """多个动态参数嵌套依赖测试示例"""
5055 # 验证原始数据
5156 assert len (raw_data ) == size
5257 assert all (item ["source" ] == data_source for item in raw_data )
53-
58+
5459 # 验证处理后的数据
5560 assert len (processed_data ) == size
56- assert all (item ["id" ] == raw_item ["id" ] for item , raw_item in zip (processed_data , raw_data ))
57-
61+ assert all (
62+ item ["id" ] == raw_item ["id" ]
63+ for item , raw_item in zip (processed_data , raw_data )
64+ )
65+
5866 # 验证结果
5967 assert isinstance (is_valid , bool )
6068
@@ -86,15 +94,17 @@ def session_scoped_data():
8694
8795class TestScopesUsage :
8896 """作用域使用测试类示例"""
89-
97+
9098 @with_dynamic_params (
9199 func_data = function_scoped_data ,
92100 class_data = class_scoped_data ,
93101 mod_data = module_scoped_data ,
94- session_data = session_scoped_data
102+ session_data = session_scoped_data ,
95103 )
96104 @pytest .mark .parametrize ("input_value" , [1 , 2 ])
97- def test_scope_management (self , input_value , func_data , class_data , mod_data , session_data ):
105+ def test_scope_management (
106+ self , input_value , func_data , class_data , mod_data , session_data
107+ ):
98108 """作用域管理测试示例"""
99109 assert func_data == f"func_{ input_value } "
100110 assert class_data == "class_shared"
@@ -118,16 +128,13 @@ def uncached_data():
118128 return time .time ()
119129
120130
121- @with_dynamic_params (
122- cached_result = cached_data ,
123- uncached_result = uncached_data
124- )
131+ @with_dynamic_params (cached_result = cached_data , uncached_result = uncached_data )
125132@pytest .mark .parametrize ("input_value" , [1 , 2 ])
126133def test_cache_control (input_value , cached_result , uncached_result ):
127134 """缓存控制测试示例"""
128135 # 验证缓存结果
129136 assert cached_result == input_value * 10
130-
137+
131138 # 验证非缓存结果(每次都不同)
132139 assert isinstance (uncached_result , float )
133140
@@ -149,16 +156,13 @@ def eager_data(input_value):
149156 return input_value * 3
150157
151158
152- @with_dynamic_params (
153- lazy_result = lazy_data ,
154- eager_result = eager_data
155- )
159+ @with_dynamic_params (lazy_result = lazy_data , eager_result = eager_data )
156160@pytest .mark .parametrize ("input_value" , [1 , 2 ])
157161def test_lazy_loading (input_value , lazy_result , eager_result ):
158162 """懒加载控制测试示例"""
159163 # 验证懒加载结果
160164 assert lazy_result == input_value * 5
161-
165+
162166 # 验证非懒加载结果
163167 assert eager_result == input_value * 3
164168
0 commit comments