添加pytest的测试函数的局部变量,在pytest-csv中单独一列 [英] Add local variables of test functions of pytest to make a separate column in pytest-csv

查看:31
本文介绍了添加pytest的测试函数的局部变量,在pytest-csv中单独一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 pytest-csv 生成的日志中添加列,并使用我的测试函数的局部变量填充它.

I am trying to add columns in the logs generated by pytest-csv and fill it using the local variables of my test function.

我的测试函数是这样的:

My test function is something like this:

def test_login(browser):
  search_page = SearchPage(browser)
  search_page.load()
  login_success = search_page.login()
  assert login_success=='Proceed'

我想在列中添加 login_success 变量的值.有一种方法可以使用 properties 和 properties_as_columns 来做到这一点,但我无法弄清楚如何做到这一点.请帮我解决这个问题.

I want to add value of login_success variable in the column. There is a way to do it using properties and properties_as_columns but I am not able to figure out how to do that. Please help me to resolve this.

推荐答案

使用 record_property 固定装置来存储值:

Use the record_property fixture to store the values:

def test_login(record_property, browser):
    search_page = SearchPage(browser)
    search_page.load()
    login_success = search_page.login()
    record_property('status of the login step', login_success)
    record_property('something else', 123)
    assert login_success == 'Proceed'

如果您现在在运行测试时选择 properties 列,则所有记录的属性都将保留在那里:

If you now select the properties column when running tests, all recorded properties will be persisted there:

$ pytest --csv out.csv --csv-columns properties
...

$ cat out.csv 
properties
"something else=123,status of the login step=Proceed"

如果您选择properties_as_columns,每个记录的名称将在 csv 报告中获得一个单独的列:

If you select properties_as_columns, each recorded name will get a separate column in the csv report:

$ pytest --csv out.csv --csv-columns properties_as_columns
...
$ cat out.csv
something else,status of the login step
123,Proceed

这篇关于添加pytest的测试函数的局部变量,在pytest-csv中单独一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆