在GNURadio(版本3.8,在Python 3.6.8上运行)中设置单元测试时,assertFloatTuplesAlmostEqual
方法似乎给出了错误的结果。
我正在研究GNURadio OutOfTreeModules教程,现在专注于单元测试。在单元测试中,我提供了预期和实际结果的向量。 (我正在测试的代码可以正常工作)。测试使用指令self.assertFloatTuplesAlmostEqual(expected_result, result_data, 6)
。 assert语句似乎只检查向量中的第一项;如果我使向量中任何其他项目的期望值与实际值不同,则测试通过。我无法在assertFloatTuplesAlmostEqual方法上找到任何文档。这是单元测试的代码:
from gnuradio import blocks
import howto1_swig as howto1
class qa_cube_ff(gr_unittest.TestCase):
def setUp(self):
self.tb = gr.top_block()
def test_001_cube_ff(self):
print("test 001 STARTING")
src_data = (-3, 4, -5.5 )
print(src_data)
# expected_result = (-27, 60, 166.375) // these would be the correct answers
# below, only the first expected result is correct; the other 2 are intentionally wrong
expected_result = (-27, 60, 100.375)
src = blocks.vector_source_f(src_data)
sqr = howto1.cube_ff()
dst = blocks.vector_sink_f()
self.tb.connect(src, sqr)
self.tb.connect(sqr, dst)
self.tb.run()
result_data = dst.data()
print("EXPECTED")
print(expected_result)
print("RESULT")
print(result_data)
self.assertFloatTuplesAlmostEqual(expected_result, result_data, 6)
def tearDown(self):
self.tb = None
if __name__ == '__main__':
gr_unittest.run(qa_cube_ff)
这是我尝试进行测试的地方。请注意,我已输入print语句以显示运行代码的预期结果和实际结果。 assetFloatTuplesAmostEqual代码仅测试向量中的第一项;如果第一个元素的预期值与实际值不匹配,则测试将失败(应如此)。其他任何元素的错误值都通过了测试-
odroid@odroid:~/projects/grc_module/gr-howto1/build$ ctest -VV -R cube
UpdateCTestConfiguration from :/home/odroid/projects/grc_module/gr-howto1/build/DartConfiguration.tcl
UpdateCTestConfiguration from :/home/odroid/projects/grc_module/gr-howto1/build/DartConfiguration.tcl
Test project /home/odroid/projects/grc_module/gr-howto1/build
Constructing a list of tests
Done constructing a list of tests
Updating test list for fixtures
Added 0 tests to meet fixture requirements
Checking test dependency graph...
Checking test dependency graph end
test 1
Start 1: qa_cube_ff
1: Test command: /bin/sh "/home/odroid/projects/grc_module/gr-howto1/build/python/qa_cube_ff_test.sh"
1: Test timeout computed to be: 9.99988e+06
1: .
1: ----------------------------------------------------------------------
1: Ran 1 test in 0.003s
1:
1: OK
1: test 001 STARTING
1: (-3, 4, -5.5)
1: EXPECTED
1: (-27, 60, 100.375)
1: RESULT
1: (-27.0, 64.0, -166.375)
1/1 Test #1: qa_cube_ff ....................... Passed 1.98 sec
The following tests passed:
qa_cube_ff
100% tests passed, 0 tests failed out of 1
Total Test time (real) = 1.99 sec
这里第一个元素是正确的(-3立方)。另外两个是错误的。
为什么? 一个相关的问题:谁能建议我在哪里可以找到有关gr_unittest方法的文档?
答案 0 :(得分:0)
assert语句似乎只检查向量中的第一项;如果我使向量中任何其他项目的期望值与实际值不同,则测试通过
这听起来像是个错误,请考虑在https://github.com/gnuradio/gnuradio/issues中进行报告
我在assertFloatTuplesAlmostEqual方法上找不到任何文档。 一个相关的问题:谁能建议我在哪里可以找到有关gr_unittest方法的文档?
有时最好的文档是源代码本身-https://github.com/gnuradio/gnuradio/blob/maint-3.8/gnuradio-runtime/python/gnuradio/gr_unittest.py#L104-L123
def assertFloatTuplesAlmostEqual(self, a, b, places=7, msg=None):
"""
Fail if the two real-valued tuples are not approximately equal.
Approximate equality is determined by specifying the number of decimal
places.
"""
self.assertEqual(len(a), len(b))
return all((
self.assertAlmostEqual(x, y, places, msg)
for (x, y) in zip(a, b)
))
def assertFloatTuplesAlmostEqual2(self, a, b,
abs_eps=1e-12, rel_eps=1e-6, msg=None):
self.assertEqual(len(a), len(b))
return all((
self.assertComplexAlmostEqual2(x, y, abs_eps, rel_eps, msg)
for (x, y) in zip(a, b)
))