vendredi 16 décembre 2016

Python ddt unittest select specific fields from test data

I'm in the process of building data driven tests in Python using unittest and ddt.

Is there a way for me to be able to select specific fields from the test data instead of having to pass all the fields as separate parameters?

For example: I have a csv file containing customers as below:

    Title,FirstName,Surname,AddressA,AddressB,AddressC,City,State,PostCode
    Mr,Bob,Gergory,44 road end,A Town,Somewhere,LOS ANGELES,CA,90004
    Miss,Alice,Woodrow,99 some street,Elsewhere,A City,LOS ANGELES,CA,90003

From this I'd like to be able to select just the first name, city and state in the test.

I can do this like below, however this seems messy and will be more with wider files:

@data(get_test_data("customers.csv"))
@unpack
def test_create_new_customer(self, Title,FirstName,Surname,AddressA,AddressB,AddressC,City,State,PostCode):
    self.customer.enter_first_name(FirstName)
    self.customer.enter_city(City)
    self.customer.enter_state_code(State)
    self.customer.click_update()

I was hoping to be able to build a dictionary list out of the csv and then access it as below:

@data(get_test_data_as_dictionary("customers.csv"))
@unpack
def test_create_new_customer(self, test_data):     
    self.customer.enter_first_name(test_data["FirstName"])
    self.customer.enter_city(test_data["City"])
    self.customer.enter_state_code(test_data["State"])
    self.customer.click_update()

However it would seem that ddt is smarter that I thought and breaks out the data from the dictionary and still expects all the parameters to be declared.

Is there a better way to achieve what I'm after?

Aucun commentaire:

Enregistrer un commentaire