首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python单元测试:测试测试

Python单元测试:测试测试
EN

Stack Overflow用户
提问于 2013-09-09 09:17:09
回答 2查看 279关注 0票数 0

我正在编写一些代码,它适用于非常大的和非常小的浮点数(例如,1e-150可能是一个有效的答案)。为了对此进行单元测试,我想将浮点数与许多重要数字进行比较,而不是小数位,因此我有以下内容。

代码语言:javascript
运行
复制
import unittest as ut
from numpy.testing import assert_approx_equal

class newTestCase(ut.TestCase):
"""Extends the basic unittest TestCase."""

def assertSFAlmostEqual(self, a, b, places=7):
    """Uses numpy to test if two floats are the same but to a defined
    number of significant figures rather than decimal places.

    Args:
        a: float to be compared
        b: float to be compared
        places: number of significant figures to match. unittest default
        for assertAlmostEqual is 7, so 7 is the default here
    """
    if isinstance(a, float) != True or isinstance(b, float) != True:
        raise TypeError

    raised = False
    try:
        assert_approx_equal(a, b, significant=places)
    except:
        raised = True
    self.assertFalse(raised, "FLoats %g and %g are not equal to %i "
                     "significant figures" % (a, b, places))

这似乎很好,但我计划在很多地方使用它,所以我想确定它确实正确工作。我的问题是,我怎样才能最明智地做到这一点?是否有适当的机制来测试单元测试?

我在这里找到了答案,

How to unittest unittest TestCases

但我不明白这是怎么回事。

非常感谢提前!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-09-09 09:35:32

unittest.TestCase的子类和任何其他类一样,因此您可以编写一个unittest.TestCase来检查它的方法是否正常工作。

特别是,您应该构建一组数字对,这些数字应该通过测试并失败,然后使用这些输入调用assertSFAlmostEqual方法,查看测试是否通过。

您所链接的答案可以做到这一点,尽管它可能是一个比所需的更复杂的解决方案。例如,我本可以简单地编写如下内容:

代码语言:javascript
运行
复制
import unittest


class MyBaseTestCase(unittest.TestCase):
    def assertSpec(self, thing):
        assert thing == 123


class TestMyTest(MyBaseTestCase):
    def test_failures(self):
        self.assertRaises(AssertionError, self.assertSpec, 121)

    def test_successes(self):
        self.assertSpec(123)

if __name__ == "__main__":
    unittest.main()

您只需子类测试用例,测试只需调用您编写的带有特定参数的assert*方法,您知道这些参数应该通过/不通过测试。

关于assert*方法当前实现的一些注意事项:

代码语言:javascript
运行
复制
if isinstance(a, float) != True or isinstance(b, float) != True:

避免与TrueFalse进行比较。在您的情况下,您可以简单地写:

代码语言:javascript
运行
复制
if not isinstance(a, float) or not isinstance(b, float):
# or:
if not (isinstance(a, float) and isinstance(b, float))

读起来也更清楚。

代码语言:javascript
运行
复制
raised = False
try:
    assert_approx_equal(a, b, significant=places)
except:
    raised = True

从不使用普通except:捕获异常。在这种情况下,您确实希望只捕获由AssertionError引发的assert_approx_equal,因此您应该使用:

代码语言:javascript
运行
复制
raised = False
try:
    assert_approx_equal(a, b, significant=places)
except AssertionError:
    raised = True

其次,您可以避免使用raised标志。try-except语句允许只在未引发异常时执行else子句:

代码语言:javascript
运行
复制
try:
    assert_approx_equal(a, b, significant=places)
except AssertionError:
    # here put the code to be executed when the assertion fails.
else:
    # here put the code to be executed when *no* exception was raised.
票数 2
EN

Stack Overflow用户

发布于 2014-01-10 18:24:26

一种方法是TDD (测试驱动开发):

  1. 写一个不及格的测试。
  2. 让代码通过测试。
  3. 重构。
  4. 去1。

这里的关键是先写一个失败的测试。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18695042

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档