Add local variables of test functions of pytest to make a separate column in pytest-csv

谁说胖子不能爱 提交于 2020-04-15 06:50:01

问题


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'

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.


回答1:


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'

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"

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


来源:https://stackoverflow.com/questions/61073934/add-local-variables-of-test-functions-of-pytest-to-make-a-separate-column-in-pyt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!