设置日志目录
A. 改变日志的根目录:testrunner.py
B. 创建用例目录:Create Log Path
一、修改RIDE源代码:testrunner.py
日志根目录结构:
RIDE中的项目预设:
打开C:\Python27\Lib\site-packages\robotide\contrib\testrunner,找到testrunner.py。
修改代码块def _create_temporary_directory(self)。如果创建目录失败,请查看个人博客修改cmd的权限。
#Need to import datetime
import datetime
#
def _create_temporary_directory(self):
# self._output_dir = tempfile.mkdtemp(".d", "RIDE")
# Create log directory which is time-based.
base = 'f:\\robotframework\\'
self._output_dir = os.path.join(base, 'logs', datetime.datetime.now().strftime('%Y%m%d_%H%M%S'))
os.mkdir(self._output_dir)
#
atexit.register(self._remove_temporary_directory)
- 注释的一行代码为RIDE临时存储目录;
- 增加的三行代码是基于当天日期时间创建目录。
修改代码块def run_commandy(self, command, cwd)
def run_command(self, command, cwd):
self._pid_to_kill = None
#Create target log dir
self._create_temporary_directory()
#
self._process = Process(cwd)
self._process.run_command(command)
二、生成用例日志文件夹关键字: Common_Test_Setup | ${TEST NAME} | ${OUTPUT DIR}
用例日志文件夹:
三、关键字 Common_Test_Setup 调用 Create Log Path
四、导入库main.py,定义了关键字 Create Log Path
目的:删除已有current目录,建立一个新的软链current到创建的基于当天日期时间的目录。
五、关键字源代码:Create Log Path
import os
import sys
class main:
def create_log_path(self, case=None, outputdir=None):
'''Create Case log Directory
Usage: | Create Log Path | caseName | outputDir |
'''
G_TEST_PATH = str(outputdir)
g_log = os.getenv('G_LOG')
print '=' * 100
print G_TEST_PATH
print '=' * 100
# Window code
base = 'f:\\robotframework\\'
os.system('rd -Q ' + '"' + os.path.join(base, 'logs', 'current') + '"')
os.system('mklink /D ' + '"' + os.path.join(base, 'logs', 'current') + '"' + ' ' + '"' + G_TEST_PATH + '"')
#
if case:
case_name = str(case).replace(' ', '_')
print 'case_name:' + case_name
G_CASE_LOG_PATH = os.path.join(G_TEST_PATH, case_name)
print 'G_CASE_LOG_PATH:' + G_CASE_LOG_PATH
if not os.path.exists(G_CASE_LOG_PATH):
os.mkdir(G_CASE_LOG_PATH)
os.environ['G_CURRENTLOG'] = str(G_CASE_LOG_PATH)
print 'G_CURRENTLOG:' + os.getenv('G_CURRENTLOG')
else:
print 'AT_ERROR : Create Log File FAIL!'
print 'AT_ERROR : ' + G_CASE_LOG_PATH + ' has exist!'
else:
print 'AT_ERROR : Create Log File FAIL!'
print 'AT_ERROR : ' + case + ' NOT Exist!'
#linux: login as root
#linux path: /root/robotframework/logs
#linux code
base = os.path.expanduser('~')
os.system('rm -rf ' + os.path.join(base, 'robotframework/logs', 'current'))
os.system("ln -s " + G_TEST_PATH + ' ' + os.path.join(base, 'robotframework/logs', 'current'))
#MAC OS: login as admin
#MAC OS path: /Users/admin/robotframework/logs
#MAC OS code
base = os.path.expanduser('~')
os.system('rm -rf ' + os.path.join(base, 'robotframework/logs', 'current'))
os.system("ln -s " + os.path.join(base, 'robotframework/logs', 'current') + ' ' + G_TEST_PATH)