1

I have read Adding a method to an existing object instance and I got it.

Now I have two directory and have some file, like this:

.
|--A
|  |--a_A.txt
|  |--b_A.txt
|--B
|  |--a_B.txt
|  |--b_B.txt

I need to compare a_A.txt and a_B.txt, b_A.txt and b_B.txt.

And I use pyunit, because A dirctory and B dirctory maybe have many file, and I want to compare at every turn that procuces a test* function, this function name is filename, like test_a_A.

I have version 1 code, like this:

def _p(lst):
    def wrapper(func):
        def action(self):
            for l in lst:
                func(self, l)
        return action
    return wrapper

class CTest(unittest.TestCase):
    expect = [] # A directory file list
    actual = [] # B directory file list

    @_p(lst=expect)
    def testAction(self, filename):
        self.do_compare(filename, actual[0]) # only example

If the code likes this, it have only one result testcase. But I have two testcase, so I need to dynamic produce testcase in addition to filename.

EDIT

Done! I use setattr to add dynamic attribute.

Community
  • 1
  • 1
thinkerou
  • 1,781
  • 5
  • 17
  • 28

0 Answers0