lundi 29 février 2016

Inheriting from unittest's TestCase - Python

I'm implementing a test automation framework. In order to make it customizable, I decided to inherit from unittest.TestCase:

import multiprocessing
import unittest
import logger
import credentials

class MyTestCase(unittest.TestCase):
    """
    An extention of the unittest's TestCase
    """
    __test__ = False # Important, makes sure tests are not run on base class

    def __init__(self, login_url=None,
                 *args, **kwargs):

        unittest.TestCase.__init__(self, *args, **kwargs)
        self.log = logger.logger
        # add a framework object for each test case.
        self.framework = None
        self.password = None
        self.credential = credentials.Credential()
        self.username = None
        self.login_url=login_url

    def do_work(self, func, args, callback, processes=1):
        """
        Perform a task asynchronously, in order to avoid delays or race conditions.

        Args:
            f: (function)
                pass a function object to the async call.
            args: (list)
                The arguments the function f take
            callback: (function)
                Where to return the method to, after completion.
            processes: (integer)

        Returns:
            The result of applying args to the function func.
        """
        try:
            pool = multiprocessing.Pool(processes=processes)
            result = pool.apply_async(func, args, callback)
            return result
        except Exception, e:
            self.log.exception(e)

before, I was passing methodName='runTest' however that was causing only def runTest(self) to be executed. A sample test is:

class SampleTest(MyTestCase):
    __test__ = True

    def __init__(self, *args, **kwargs):
       MyTestCase.__init__(self, *args, **kwargs)

    def setUp(self):
      # do some setting up here

    def runTest(self):
       self.test_a()
       self.test_b()

    def test_a(self):
       # to some testing A here

    def test_b(self):
       # to some testing B here

So the XML test-reports were only showing runTest() as executed. I want to modify that and since I don't need runTest() much, I would like to have test_a() and test_b() executing as standalone tests. However when I execute the code, I'm getting the following error:

2016-02-29 17:12:04 - ERROR - no such test method in <class 'testcases.sample_test.SampleTest'>: runTest
Traceback (most recent call last):
  File "sample_test.py", line 27, in __init__
    testcase.MyTestCase.__init__(self, *args, **kwargs)
  File "my_case.py", line 29, in __init__
    unittest.TestCase.__init__(self, *args, **kwargs)
  File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 189, in __init__
    (self.__class__, methodName))
ValueError: no such test method in <class 'sample_test.SampleTest'>: runTest

Sure, adding methodName='runTest' to the MyTestCase constructor will fix that ... however that will not produce the result I want. How can I have it fixed?

Aucun commentaire:

Enregistrer un commentaire