0

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.

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

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). 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!)

my guess is to just use more for loops to pull out ever possibility. 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):

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))

3 Answers 3

0

A simpler approach would be to use unittest's assertItemsEqual, which is designed for exactly this purpose. Given O.P.'s a, b, & c nested lists:

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
0

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?

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.