shivank01
shivank01

Reputation: 1065

Add local variables of test functions of pytest to make a separate column in 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'

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.

Upvotes: 1

Views: 411

Answers (1)

hoefling
hoefling

Reputation: 66431

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

Upvotes: 2

Related Questions