【问题标题】:Capture behave outputs into a dynamically created log file将行为输出捕获到动态创建的日志文件中
【发布时间】:2015-07-28 15:14:31
【问题描述】:

我正在尝试将 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 的新手,因此期待有关如何实现它的任何解决方案/建议。任何相关材料或信息也将不胜感激。

提前致谢

【问题讨论】:

  • 你没有解释你为什么要这么做。不,在生产中使用-o filename 并没有错(它不使用标准输出,它用filesrteam 替换标准输出)。哎呀,甚至支持更适合进一步解析的输出格式,例如--juni
  • 我试图在每次运行 Behave 时在动态创建的文件中捕获行为输出。上面给出的代码只是用于创建文件,而使用“behave -o”,必须明确提及文件名,因此不愿意使用它。只需要一些线索/想法来将be\have输出路由到动态创建的文件而不是STDOUT。
  • 我确实给了你一个。无需重新连接整个步骤,只需重新连接您的测试运行器以使用提供给 -o 的随机名称。

标签: python python-3.x python-behave


【解决方案1】:

可能是这样的,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(

【讨论】:

    猜你喜欢
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 2020-02-28
    • 2012-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多