mercredi 23 septembre 2015

Add python unit tests dynamically for Jenkins

I have ~5k test files and a python script run by an existing Jenkins job. Current script ( shown below ) runs with ~30 test files.

class MyTestKlass(unittest):
    // class methods

  @test_method_1(self):
    // test file 1
  @test_method_2(self):
    // test file 2
         ....
  @test_method_30(self):
    // test file 30

I would definitely like to avoid implementing ~5k similar methods in the script and instead implement a dynamic method generator that will add test_method_1(), test_method_2.. test_method_5000 to MyTestKlass:

class MyTestKlass(unittest):
   @classmethod
   def setUp(cls):
     MyTestKlass.generator()

   @classmethod
   def generator(cls):
     for idx, f_name in enumerate(os.listdir(TEST_FILES_DIR)):
       setattr(MyTestKlass, 'test_method_{x}'.format(idx), test_func)

How can this be done? Please excuse brevity or incorrectness of my code samples. The closest discussion I have found is this one - dynamically-generating-python-test-cases but the key problem in my case is Jenkins running the script thus I don't have a way to invoke test case generator from if __name__ == __main__ as explained in the blog.

Thanks,

Aucun commentaire:

Enregistrer un commentaire