Pain point analysis:
When we After each use case execution, if there is a bug or a test script is a problem, usually by test reports, abnormal screenshots, logs to locate the analysis, we found that after running several times, the abnormal screenshots and test reports constantly increased, resulting in positioning when we analyze the problem some trouble, because we want to advance before each use case to clean out files in the corresponding directory execution.
Solution:
1. For the files in the directory allure, because the name and file format are different, so can not be classified delete, so we want to introduce shutil module
import statement the shutil
Use shutil.rmtree (path) method to delete all the contents of the file under the path (including directories, recursive directory, files), this method please use caution!
2. For images shot under abnormal OutPuts, use the above method is not recommended, because involves a lot of catalogs and other documents, unusual pictures are shot .png format, so you can delete Sort by introducing os module
import os
Use os.unlink (path) method to delete the files in the path
Specific implementation method, see the following code:
import statement os import statement the shutil # With Clear OutPuts directory shot a picture before abnormal cases of execution MAIN_DIR = os.path.dirname (os.path.abspath ( __FILE__ )) # project root directory OUTPUTS_DIR = os.path.join (MAIN_DIR, ' OutPuts ' ) # OutPuts directory os .chdir (OUTPUTS_DIR) # switch to OutPuts directory try: shutil.rmtree ( ' allure ' ) # empty files in the directory allure the except FileNotFoundError AS E: Print (F ' Reports directory does not exist, as detailed below: \ n-E {} ' ) for I in the os.listdir (OUTPUTS_DIR): IF ' PNG ' in I: os.unlink(i) os.chdir (MAIN_DIR) # to switch to the root directory of the project