Unit testing method that repeats through loops

I am trying to find a good way to test a method that goes through some loops and then gets an object and calls one of its methods. This class has its own tests, so I'm not sure exactly what to test here. I wrote this to have different behavior in a test case, which is obviously not ideal.

I am looking for suggestions on how to improve this for testing without conventions.

def direct(self, test=False): """ routes data in self.data_groups to consumers in self.consumers_list """ if test: output_list = [] for data_type, group in self.data_groups.items(): if test: output_list.append(data_type) output_list.append(group) for consumer_name in self.consumers_list[data_type]: for record in group: if test: output_list.append(record.values()[0]) else: consumer = self.get_consumer(consumer_name, record) consumer_output = consumer.do_something() if test: return output_list return True 
+4
source share
2 answers

Unfortunately, I'm not sure what you are talking about is possible. I would say that you can use a decorator, but that would be useless without a complete redefinition. I'm not sure if this is the best for you, but can you just override the method for your tests in your class? That would make it a lot cleaner and you could group your code in a more intuitive way.

do something like:

 class DirectClass: def __init__(self): self.data_groups = dict self.consumers_list = list def direct(self): """ routes data in self.data_groups to consumers in self.consumers_list """ for data_type, group in self.data_groups.items(): for consumer_name in self.consumers_list[data_type]: for record in group: consumer = self.get_consumer(consumer_name, record) consumer_output = consumer.do_something() return True class TestDirect(DirectClass): def __init__(self): DirectClass.__init__(self) def direct(self): output_list = [] for data_type, group in self.data_groups.items(): output_list.append(data_type) output_list.append(group) for consumer_name in self.consumers_list[data_type]: for record in group: output_list.append(record.values()[0]) return output_list 
+3
source

I settled on subclassing the object returned by the consumer so that I could call the same code and have a processed over-ridden run method (previously do_something) that returns test data. There is more than I want, but it reaches most of my goal. Get @ user2916286 for thinking of a subclass in this case.

 def direct(self, test=False): """ routes data in self.data_groups to consumers in self.consumers_list """ if test: output_list = [] for data_type, group in self.data_groups.items(): if test: output_list.append(data_type) output_list.append(group) for consumer_name in self.consumers_list[data_type]: for record in group: consumer = self.get_consumer(consumer_name, record, test=test) consumer_output = consumer.run() if not consumer_output: raise Exception('consumer failed') output_list.append(consumer_output) if test: return output_list return True 
+1
source

All Articles