Python测试驱动编写

通过gr_modtool会自动生成测试驱动程序的模板,只需要修改模板即可,模板如下:

from gnuradio import gr, gr_unittest
from gnuradio import blocks
import double_diff_swig as double_diff

class qa_translate (gr_unittest.TestCase):

    def setUp (self):
        self.tb = gr.top_block ()

    def tearDown (self):
        self.tb = None

    def test_001_t (self):
        # set up fg
        self.tb.run ()
        # check data


if __name__ == '__main__':
    gr_unittest.run(qa_translate, "qa_translate.xml")
这里module的名称为double_diff,block的名称为translate。

我们需要做的就是补充test_001_t()函数:构建流图、给出测试数据以及预期结果、运行流图、将实际结果与预期结果I进行比较。修改好的程序如下:

from gnuradio import gr, gr_unittest
from gnuradio import blocks
import double_diff

class qa_translate (gr_unittest.TestCase):

    def setUp (self):
        self.tb = gr.top_block ()

    def tearDown (self):
        self.tb = None

    def test_001_t (self):
	src_data=[1,-1,-1,1,1,-1]
	expected_ampl=(1,1,1,1,1,1)
	expected_phase=(0,1,1,0,0,1)

	src=blocks.vector_source_c(src_data)
	op=double_diff.translate()
	dst0=blocks.vector_sink_f()
	dst1=blocks.vector_sink_f()

	self.tb.connect(src,op)
	self.tb.connect((op,0),dst0)
	self.tb.connect((op,1),dst1)
        # set up fg
        self.tb.run ()
        # check data
	result_ampl=dst0.data()
	result_phase=dst1.data()
	self.assertEqual(expected_ampl,result_ampl)
	self.assertEqual(expected_phase,result_phase)


if __name__ == '__main__':
    gr_unittest.run(qa_translate, "qa_translate.xml")
运行结果:


你可能感兴趣的:(单元测试)