Unit Testing Interfaces in Python

2024/11/13 12:19:06

I am currently learning python in preperation for a class over the summer and have gotten started by implementing different types of heaps and priority based data structures.

I began to write a unit test suite for the project but ran into difficulties into creating a generic unit test that only tests the interface and is oblivious of the actual implementation.

I am wondering if it is possible to do something like this..

suite = HeapTestSuite(BinaryHeap())
suite.run()
suite = HeapTestSuite(BinomialHeap())
suite.run()

What I am currently doing just feels... wrong (multiple inheritance? ACK!)..

class TestHeap:def reset_heap(self):self.heap = Nonedef test_insert(self):self.reset_heap()#test that insert doesnt throw an exception...for x in self.inseq:self.heap.insert(x)def test_delete(self):#assert we get the first value we put inself.reset_heap()self.heap.insert(5)self.assertEquals(5, self.heap.delete_min())#harder test. put in sequence in and check that it comes out rightself.reset_heap()for x in self.inseq:self.heap.insert(x)for x in xrange(len(self.inseq)):val = self.heap.delete_min()self.assertEquals(val, x)class BinaryHeapTest(TestHeap, unittest.TestCase):def setUp(self):self.inseq = range(99, -1, -1)self.heap = BinaryHeap()def reset_heap(self):self.heap = BinaryHeap()class BinomialHeapTest(TestHeap, unittest.TestCase):def setUp(self):self.inseq = range(99, -1, -1)self.heap = BinomialHeap()def reset_heap(self):self.heap = BinomialHeap()if __name__ == '__main__':unittest.main()
Answer

I personally like nose test generation more for this sort of thing. I'd then write it like:

# They happen to all be simple callable factories, if they weren't you could put
# a function in here:
make_heaps = [BinaryHeap, BinomialHeap]def test_heaps():for make_heap in make_heaps:for checker in checkers: # we'll set checkers lateryield checker, make_heapdef check_insert(make_heap):heap = make_heap()for x in range(99, -1, -1):heap.insert(x)# def check_delete_min etc.checkers = [valuefor name, value in sorted(globals().items())if name.startswith('check_')]
https://en.xdnf.cn/q/72297.html

Related Q&A

Python Pandas average based on condition into new column

I have a pandas dataframe containing the following data:matchID server court speed 1 1 A 100 1 2 D 200 1 3 D 300 1 …

Merging same-indexed rows by taking non-NaNs from all of them in pandas dataframe

I have a sparse dataframe with duplicate indices. How can I merge the same-indexed rows in a way that I keep all the non-NaN data from the conflicting rows?I know that you can achieve something very c…

Approximating cos using the Taylor series

Im using the Taylors series to calculate the cos of a number, with small numbers the function returns accurate results for example cos(5) gives 0.28366218546322663. But with larger numbers it returns i…

How to apply max min boundaries to a value without using conditional statements

Problem:Write a Python function, clip(lo, x, hi) that returns lo if x is less than lo; hi if x is greater than hi; and x otherwise. For this problem, you can assume that lo < hi.Dont use any conditi…

pandas to_json() redundant backslashes

I have a .csv file containing data about movies and Im trying to reformat it as a JSON file to use it in MongoDB. So I loaded that csv file to a pandas DataFrame and then used to_json method to write i…

How can I get the old zip() in Python3?

I migrated from Python 2.7 to Python 3.3 and zip() does not work as expected anymore. Indeed, I read in the doc that it now returns an iterator instead of a list.So, how I am supposed to deal with this…

How can I use tensorflow metric function within keras models?

using python 3.5.2 tensorflow rc 1.1Im trying to use a tensorflow metric function in keras. the required function interface seems to be the same, but calling:import pandas import numpy import tensorflo…

Pandas return the next Sunday for every row

In Pandas for Python, I have a data set that has a column of datetimes in it. I need to create a new column that has the date of the following Sunday for each row. Ive tried various methods trying to u…

Where is `_softmax_cross_entropy_with_logits` defined in tensorflow?

I am trying to see how softmax_cross_entropy_with_logits_v2() is implemented. It calls _softmax_cross_entropy_with_logits(). But I dont see where the latter is defined. Does anybody know how to locate …

Python: Counting frequency of pairs of elements in a list of lists

Actually, I have a dataset about a "meeting". For example, A,B,C have a meeting, then the list would be [A,B,C]. Like this, each list would contain a list of members who participated in the …