Python中的单元测试接口

编程入门 行业动态 更新时间:2024-10-12 01:29:45
本文介绍了Python中的单元测试接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我目前正在为夏季的课程学习python,并且已经开始实施不同类型的堆和基于优先级的数据结构。

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 = None def 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 in self.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 right self.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()

推荐答案

我个人喜欢鼻子测试生成更多这类事情。然后我会写它:

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 later yield checker, make_heap def check_insert(make_heap): heap = make_heap() for x in range(99, -1, -1): heap.insert(x) # def check_delete_min etc. checkers = [ value for name, value in sorted(globals().items()) if name.startswith('check_')]

更多推荐

Python中的单元测试接口

本文发布于:2023-11-24 02:26:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1623630.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单元测试   接口   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!