33Provides convenient Allure reporting functionality and decorators
44"""
55
6- import allure
76import os
8- import pytest
9- import subprocess
10- import shutil
11- import time
127import platform
8+ import shutil
9+ import subprocess
1310import sys
11+ import time
1412from pathlib import Path
15- from typing import Dict , Any , ContextManager , Optional , Union , List
16-
13+ from typing import Any , ContextManager , Dict , List , Optional , Union
1714
15+ import allure
16+ import pytest
1817
1918
2019def setup_allure (config : Dict [str , Any ]) -> Optional [Path ]:
@@ -26,8 +25,10 @@ def setup_allure(config: Dict[str, Any]) -> Optional[Path]:
2625 base_dir = Path (config .get ("base_dir" , "reports" ))
2726 if config .get ("use_timestamp" , False ) and base_dir .exists ():
2827 timestamp_dirs = [
29- d for d in base_dir .iterdir ()
30- if d .is_dir () and d .name .startswith (config .get ("directory_prefix" , "pytest" ))
28+ d
29+ for d in base_dir .iterdir ()
30+ if d .is_dir ()
31+ and d .name .startswith (config .get ("directory_prefix" , "pytest" ))
3132 ]
3233 if timestamp_dirs :
3334 timestamp_dirs .sort (key = lambda x : x .stat ().st_mtime , reverse = True )
@@ -37,8 +38,8 @@ def setup_allure(config: Dict[str, Any]) -> Optional[Path]:
3738 allure_dir .mkdir (parents = True , exist_ok = True )
3839 os .environ ["ALLURE_REPORT_DIR" ] = str (allure_dir )
3940
40- env_info = _get_system_info ()
41- custom_env = allure_cfg .get ("environment" , {})
41+ env_info = _get_system_info ()
42+ custom_env = allure_cfg .get ("environment" , {})
4243 env_info .update (custom_env )
4344 _create_environment_properties (allure_dir , env_info )
4445
@@ -56,7 +57,7 @@ def check_allure_available() -> bool:
5657 capture_output = True ,
5758 text = True ,
5859 timeout = 10 ,
59- shell = True
60+ shell = True ,
6061 )
6162 return result .returncode == 0
6263 except Exception :
@@ -67,11 +68,13 @@ def serve_allure_report(
6768 allure_results_dir : Union [str , Path ],
6869 host : str = "localhost" ,
6970 port : int = 8080 ,
70- auto_open : bool = True
71+ auto_open : bool = True ,
7172) -> Optional [subprocess .Popen ]:
7273 """Start Allure server and optionally open browser."""
7374 if not check_allure_available ():
74- print ("Allure CLI not found. Install from https://github.com/allure-framework/allure2/releases" )
75+ print (
76+ "Allure CLI not found. Install from https://github.com/allure-framework/allure2/releases"
77+ )
7578 return None
7679
7780 allure_results_dir = Path (allure_results_dir )
@@ -90,7 +93,7 @@ def serve_allure_report(
9093 stderr = subprocess .STDOUT ,
9194 text = True ,
9295 bufsize = 1 ,
93- universal_newlines = True
96+ universal_newlines = True ,
9497 )
9598 print (f"Allure server starting at http://{ host } :{ port } (PID: { process .pid } )" )
9699 print ("Please press Ctrl+C to stop the server" )
@@ -118,11 +121,13 @@ def generate_allure_html(
118121 allure_results_dir : Union [str , Path ],
119122 html_output_dir : Optional [Union [str , Path ]] = None ,
120123 clean : bool = False ,
121- auto_serve : bool = False
124+ auto_serve : bool = False ,
122125) -> Optional [Union [Path , subprocess .Popen ]]:
123126 """Generate static HTML report or serve dynamically."""
124127 if not check_allure_available ():
125- print ("Allure CLI not found. Install from https://github.com/allure-framework/allure2/releases" )
128+ print (
129+ "Allure CLI not found. Install from https://github.com/allure-framework/allure2/releases"
130+ )
126131 return None
127132
128133 allure_results_dir = Path (allure_results_dir )
@@ -133,13 +138,17 @@ def generate_allure_html(
133138 if auto_serve :
134139 return serve_allure_report (allure_results_dir )
135140
136- html_output_dir = Path (html_output_dir or allure_results_dir .parent / "allure-report" )
141+ html_output_dir = Path (
142+ html_output_dir or allure_results_dir .parent / "allure-report"
143+ )
137144 if clean and html_output_dir .exists ():
138145 shutil .rmtree (html_output_dir )
139146 html_output_dir .mkdir (parents = True , exist_ok = True )
140147
141148 allure_path = shutil .which ("allure" )
142- cmd = f'{ allure_path } generate "{ allure_results_dir } " -o "{ html_output_dir } " --clean'
149+ cmd = (
150+ f'{ allure_path } generate "{ allure_results_dir } " -o "{ html_output_dir } " --clean'
151+ )
143152 result = subprocess .run (cmd , shell = True , capture_output = True , text = True , timeout = 60 )
144153
145154 if result .returncode == 0 :
@@ -150,17 +159,18 @@ def generate_allure_html(
150159 return None
151160
152161
153- def _create_environment_properties (allure_results_dir : Union [str , Path ],
154- environment_info : Dict [str , str ]) -> None :
162+ def _create_environment_properties (
163+ allure_results_dir : Union [str , Path ], environment_info : Dict [str , str ]
164+ ) -> None :
155165 allure_results_dir = Path (allure_results_dir )
156166 allure_results_dir .mkdir (parents = True , exist_ok = True )
157-
167+
158168 env_file = allure_results_dir / "environment.properties"
159-
160- with open (env_file , 'w' , encoding = ' utf-8' ) as f :
169+
170+ with open (env_file , "w" , encoding = " utf-8" ) as f :
161171 for key , value in environment_info .items ():
162172 f .write (f"{ key } ={ value } \n " )
163-
173+
164174 print (f"Environment properties file created: { env_file } " )
165175
166176
@@ -173,7 +183,7 @@ def _get_system_info() -> Dict[str, str]:
173183 info ["OS" ] = os_name
174184
175185 # ---------- Architecture ----------
176- arch = platform .architecture ()[0 ] # '64bit' / '32bit'
186+ arch = platform .architecture ()[0 ] # '64bit' / '32bit'
177187 info ["Architecture" ] = "64-bit" if "64" in arch else "32-bit"
178188
179189 # ---------- Python ----------
@@ -191,4 +201,4 @@ def _get_system_info() -> Dict[str, str]:
191201 else :
192202 info ["Processor" ] = proc .split ()[0 ] if proc else "Kunpeng"
193203
194- return info
204+ return info
0 commit comments