English 中文(简体)
浮标 t的试验主张
原标题:Test assertions for tuples with floats

I have a function that returns a tuple that, among others, contains a float value. Usually I use assertAlmostEquals to compare those, but this does not work with tuples. Also, the tuple contains other data-types as well. Currently I am asserting every element of the tuple individually, but that gets too much for a list of such tuples. Is there any good way to write assertions for such cases? Consider this function:

def f(a):
    return [(1.0/x, x * 2) for x in a]

我现在要作一个考验:

def testF(self):
    self.assertEqual(f(range(1,3)), [(1.0, 2), (0.5, 4)])

否则将失败,因为<代码>1.0/2<>>>/代码>并非确切的<代码>0.5/code>。 任何人能否以可读的方式提出这种说法?

<<>Edit>/strong>: 实际1.0/2>确切为0.5,但你有我的意思。

最佳回答

• 如何使你的工作与 couple合起来:

def testF(self):
    for tuple1, tuple2 in zip(f(range(1,3)), [(1.0, 2), (0.5, 4)]):
        for val1, val2 in zip(tuple1, tuple2):
            if type(val2) is float:
                self.assertAlmostEquals(val1, val2, 5)
            else:
                self.assertEquals(val1, val2)

我在此的前提是,最好在一旁使用多条主张,以获得它打破的准确价值,而使用所有(所有)的单一主张。

页: 1 如果你有其他数字类型,你想要用心 差不多,您可以将以上数字改为[float, decimal],例如if category(val2)。 Decimal]:

问题回答

我或许会确定一项休养职能。

from collections import Iterable;

def recursiveAssertAlmostEqual(testCase, first, second, *args, **kwargs):
   if isinstance(first, Iterable) and isinstance(second, Iterable):
      for a, b in zip(first, second):
         recursiveAssertAlmostEqual(testCase, a, b, *args, **kwargs)
   else:
      testCase.assertAlmostEqual(first, second, *args, **kwargs)

(说明它将坚持<代码>(1、2)和[1、2]。)

我过去所做的是,写上一种确定复杂数据类型有效性的习惯功能,然后使用<代码>(IsFooValid( foo)>。 有效性职能可以简单地恢复真实/真实性,但通常最好是用适当的信息来提高阿瑟普兰。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签