python - How can I configure pytest to generate useful names when generating tests? -


i'm using py.test execute suite of selenium tests. i'm running collector in conftest.py generates tests (i stole pytest documentation):

def pytest_generate_tests(metafunc):     funcarglist = metafunc.cls.cases[metafunc.function.__name__]     argnames = list(funcarglist[0])     metafunc.parametrize(argnames, [[funcargs[i] in argnames]                                 funcargs in funcarglist]) 

my test cases placed in objects this:

class testobject(object):  def __init__(         self,         parameter_1,  ):     self.parameter_1 = parameter_1     self.parameter_2 = parameter_2 

i instantiate them this:

test_cases_values = {     "friendly_case_name_1": testobject(         "parameter_1_value",         "parameter_2_value"     ),     "friendly_case_name_2": testobject(         "parameter_1_value",         "parameter_2_value"     ), } 

my browsers attached grid server, make list of them this:

browsers = [     "('browser_1', server_url)",     "('browser_2', server_url)" ] 

i store target environment in config file instantiation of object this:

class environment(object):      def __init__(self, url=url, port=port):         self.url = url         self.port = port      def __name__(self):         return self.url + ":" + self.port  environment = environment() 

then have test class creates list of test cases - test object parameters strings allow self generating code. i'm oversimplifying pass them in fill ins broader exec statements:

class testclass(object):      cases = {"test_function": []}     in test_cases.values():         j in browsers:             cases["test_function"].append(                 dict(                     browser=j,                     environment=environment                     test_object=i                 )             )      @pytest.mark.run()     def test_function(         self,         browser,         environment,         test_object     ):         exec(test_object.parameter_1)         exec(test_object.parameter_2)         assert my_assertion 

when collector runs, looks this:

collected # items     <module 'tests.py'>       <class 'testclass'>         <instance '()'>           <function "test_function[environment0-test_object0-('browser_1', grid_server)]">           <function "test_function[environment1-test_object1-('browser_2', grid_server)]">           <function "test_function[environment2-test_object2-('browser_1', grid_server)]">           <function "test_function[environment3-test_object3-('browser_2', grid_server)]"> 

i want have collector work in such way useful information each item - i've messed around setting __str__, __repr__, , __name__ methods in various places haven't had results expected. i'd able roll reporting - there on 200 tests generates in production , have trace through stack traces figure out being tested each failure.

i'm not sure i'm making mistakes here, should modify implementation of pytest_generate_tests, or way i'm creating testclass, or set cases in different way? ideally, want can mapped via orm include test metadata well.

well, figured out. turns out metafunc.parametrize function accepts "ids" parameter. had define __repr__ of objects looking name, , expanded list comprehension return 2 things same loop.

def pytest_generate_tests(metafunc):     funcarglist = metafunc.cls.cases[metafunc.function.__name__]     argnames = list(funcarglist[0])     argvalues = []     ids = []     in funcarglist:         inner_argvalues_list = []         inner_ids_list = []         j in argnames:             inner_argvalues_list.append(i[j])             if type(i[j]) != str:                 inner_ids_list.append(i[j].__repr__())             else:                 inner_ids_list.append(i[j])         argvalues.append(inner_argvalues_list)         ids.append(inner_ids_list)     metafunc.parametrize(argnames, argvalues, ids=ids) 

Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - Chrome Extension: Interacting with iframe embedded within popup -