简体   繁体   English

如何多次测试和记录-nosetest unittest-Python

[英]How to test and log multiple times -nosetest unittest - python

Here is a mini version of problem 这是问题的迷你版

a = [[1,2,3][0,0,0]]
b = [[1,0,1][0,0,0]]
c = [[0,0,0][1,0,1]]

so if level 1 is [] and level 2 is [[]], then what i'm trying to do is test ever list to see if there level 2's match (regardless of order) so in this case b,c are equivalent. 因此,如果1级是[]而2级是[[]],那么我要尝试的是测试列表以查看是否存在2级匹配(无论顺序如何),因此在这种情况下b,c是等效的。

i'm using unittesting and nosetests to run them If i wanted to just test one table against another i would do something like: 我正在使用单元测试和鼻子测试来运行它们,如果我只想对另一个表进行测试,我会做类似的事情:

the function truth() creates my tables 函数true()创建我的表

def test_table1_table2(self):
    for row in truth(1,3):
        self.assertIn(row,truth(2,4))

but My goal is to test all tables against all other tables i have created (about 20 and growing). 但我的目标是对照我创建的所有其他表(大约20个并在不断增长)测试所有表。 Some issues i can't work out (i'm not sure if i need to read unittest docs or nosetests or don't even need them!) 有些问题我无法解决(我不确定是否需要阅读unittest文档或鼻子测试,甚至不需要它们!)

my guess is to just use more for loops to pull out ever possibility. 我的猜测是仅使用更多的for循环来消除任何可能性。 But using 但是使用

>>> nosetest 

with a 与一个

assertIn 

just stops at the first error, which isn't what i want. 只是在第一个错误处停止,这不是我想要的。 i need to scan and collect info on which lists are equivalent at (regardless of order or the nested lists). 我需要扫描并收集有关哪些列表等效的信息(无论顺序或嵌套列表如何)。 maybe i should just create something and forget about unittests? 也许我应该只是创建一些东西而忘记单元测试?

so my preferred output would be something like 所以我的首选输出将是这样的

table1 and table2 are not equivalent 
table1 and table2 are not equivalent

or probable more useful and shorter would just be 或可能更有用且更短

table1 and table10 are equivalent

Here is the code i have currently, almost everything is just a integer there expect truth() which makes the truth table (nested list): 这是我目前拥有的代码,几乎所有东西都只是一个整数,期望的是true(),它使真值表(嵌套列表):

114     def test_all(self):$                                                  |~                      
115         ''' will test all tables'''$                                      |~                      
116         for expression in range(self.count_expressions()):$               |~                      
117             num_var = count_vars(exp_choose(expression))$                 |~                      
118             for row in truth(expression, num_var):$                       |~                      
119                 for expression2 in range(self.count_expressions()):$      |~                      
120                     num_var2 = count_vars(exp_choose(expression2))$       |~                      
121                     for row2 in truth(expression2, num_var2):$            |~                      
122                         self.assertIn(row, truth(expression2,num_var2))

Try this approach: Instead of a loop inside the test method, run the outer loops outside the test class. 尝试这种方法:在测试类外部运行外部循环,而不是在测试方法内部循环。 The inner code should add new test methods to the test class. 内部代码应在测试类中添加新的测试方法。

See here for an example: How to generate dynamic (parametrized) unit tests in python? 请参见此处的示例: 如何在python中生成动态(参数化)单元测试?

Updated to a shorter solution: 更新为更短的解决方案:

def match(a,b):
  aSets = map(set, a)
  bSets = map(set, b)
  return ((aSets[0] == bSets[0]) and (aSets[1] == bSets[1])) \
      or ((aSets[0] == bSets[1]) and (aSets[1] == bSets[0]))

tables = {}
tables['a'] = [[1,2,3],[0,0,0]]
tables['b'] = [[1,0,1],[0,0,0]]
tables['c'] = [[0,0,0],[1,0,1]]
tables['d'] = [[0,0,0],[1,1,0]]

for key1, value1 in tables.items():
  for key2, value2 in tables.items():
    result = 'equivalent' if match(value1,value2) else 'not eqivalent'
    print '%s and %s are %s' % (key1, key2, result)

This could arguably be done a lot more elegantly using functions like map and zip but the point is to break down the code in smaller fragments and reduce the depth of your loop. 使用mapzip等功能可以说可以更优雅地完成此操作,但是重点是将代码分解成较小的片段并减少循环的深度。

A simpler approach would be to use unittest's assertItemsEqual , which is designed for exactly this purpose. 一种更简单的方法是使用unittest的assertItemsEqual ,它正是为此目的而设计的。 Given OP'sa, b, & c nested lists: 给定OP'sa,b和c嵌套列表:

class CheckingEqualityIgnoreOrder(unittest.TestCase):
    def testAB(self):
        self.assertItemsEqual(a, b)
        #fails

    def testBC(self):
        self.assertItemsEqual(b, c)
        #passes

    def testAC(self):
        self.assertItemsEqual(a, c)
        #fails

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM