0

我正在尝试将 Behave 输出捕获到一个文件中(比如说一个日志文件)。我在“@then”步骤为每次运行基于日期时间的行为动态创建一个新的日志文件。下面是 steps/xx.py 文件中给出的示例代码。

def filecreation(filename):
    chwd=os.chdir('C:\\Users\\xxx\\Desktop\\features\\test_features')
    with open(filename, 'w+') as d:
        pass

cur_ts = datetime.datetime.now()
log_time_stamp = str(cur_ts).split('.')[0].replace(' ',':').replace('-',':').replace(':','')
file_name = 'ATMorFrameRelay' + log_time_stamp + '.log'
filecreation(file_name)
pass

现在我试图在每次运行时将 Behave 输出发送到上面创建的日志文件。我知道命令“Behave -o [file name]”将为每次运行创建一个文件,但我认为每次新运行都会将 STDOUT 发送到上面创建的文件。使用 STDOUT 在类似生产的环境中写入文件并且不会引起任何问题是否更好/更安全。

我是 Python 和 Behave 的新手,因此期待任何关于如何实现它的解决方案/建议。任何相关材料或信息也将不胜感激。

提前致谢

4

1 回答 1

0

也许像这样,cmd实际运行测试的行为命令在哪里。

cmd = [
    'behave',
    '--no-capture',
    '--no-capture-stderr',
    '--format', 'progress2',
    '--logging-level', 'INFO',
    '--no-source', '--no-skipped', '--no-summary',
    '--tags', 'MACRO'
    ]
with io.open(filename, 'a') as writer, io.open(filename, 'rb', 1) as reader:
   process = subprocess.Popen(cmd, env=env, stdout=writer,stderr=writer)
   while process.poll() is None:
        sys.stdout.write(reader.read())
        sys.stdout.flush()
        time.sleep(0.1)
   sys.stdout.write(reader.read())
   sys.stdout.flush(
于 2017-06-26T08:51:29.970 回答