def data_driven_tests(cls): ddt.ddt(cls) for name, func in list(cls.__dict__.items()): if hasattr(func, _JSON_METADATA_ATTR): file_attr = getattr(func, _JSON_METADATA_ATTR) with codecs.open(file_attr, 'r', 'utf-8') as f: data = json.load(f) _add_tests_from_data(cls, name, func, data) delattr(cls, name) # removes original test method return cls
def test_ddt_data_name_attribute(): """ Test the ``__name__`` attribute handling of ``data`` items with ``ddt`` """ def hello(): pass class Myint(int): pass class Mytest(object): pass d1 = Myint(1) d1.__name__ = "data1" d2 = Myint(2) data_hello = data(d1, d2)(hello) setattr(Mytest, "test_hello", data_hello) ddt_mytest = ddt(Mytest) assert_is_not_none(getattr(ddt_mytest, "test_hello_1_data1")) assert_is_not_none(getattr(ddt_mytest, "test_hello_2_2"))
def test_ddt_data_name_attribute(): """ Test the ``__name__`` attribute handling of ``data`` items with ``ddt`` """ def hello(): pass class myint(int): pass class mytest(object): pass d1 = myint(1) d1.__name__ = 'data1' d2 = myint(2) data_hello = data(d1, d2)(hello) setattr(mytest, 'test_hello', data_hello) ddt_mytest = ddt(mytest) assert_is_not_none(getattr(ddt_mytest, 'test_hello_data1')) assert_is_not_none(getattr(ddt_mytest, 'test_hello_2'))
def build_suite(data: (list, dict), functions=None) -> unittest.TestSuite: if isinstance(data, list): data = format_data(data) name = data.get('name', '') config = data.get('config', {}) testcases = data.get('testcases', []) """组装suite""" class TestApi(unittest.TestCase): @classmethod def setUpClass(cls): nonlocal config cls.functions = functions or {} cls.session = requests.session() context = config.get('variables') config = parse_function(context, cls.functions, config) config_request = config.get('request') cls.base_url = config_request.pop( 'base_url') if 'base_url' in config_request else None cls.context = config.get('variables', {}) for key, value in config_request.items(): setattr(cls.session, key, value) for index, test in enumerate(testcases): # test = test.get('test') test_method = build_case(index, test) setattr(TestApi, f'test_api_{index+1}', test_method) TestApi = ddt.ddt(TestApi) TestApi.__doc__ = name # suite名 suite = unittest.defaultTestLoader.loadTestsFromTestCase(TestApi) return suite
def test_ddt_data_name_attribute(): """ Test the ``__name__`` attribute handling of ``data`` items with ``ddt`` """ def hello(): pass class Myint(int): pass class Mytest(object): pass d1 = Myint(1) d1.__name__ = 'data1' d2 = Myint(2) data_hello = data(d1, d2)(hello) setattr(Mytest, 'test_hello', data_hello) ddt_mytest = ddt(Mytest) assert_is_not_none(getattr(ddt_mytest, 'test_hello_1_data1')) assert_is_not_none(getattr(ddt_mytest, 'test_hello_2_2'))
def test_ddt_data_doc_attribute(): """ Test the ``__doc__`` attribute handling of ``data`` items with ``ddt`` """ def hello(): """testFunctionDocstring {6} :param: None :return: None """ pass class Myint(int): pass class Mytest(object): pass d1 = Myint(1) d1.__name__ = 'case1' d1.__doc__ = """docstring1""" d2 = Myint(2) d2.__name__ = 'case2' data_hello = data(d1, d2)(hello) setattr(Mytest, 'test_hello', data_hello) ddt_mytest = ddt(Mytest) assert_equal(getattr(getattr(ddt_mytest, 'test_hello_1_case1'), '__doc__'), d1.__doc__) assert_equal(getattr(getattr(ddt_mytest, 'test_hello_2_case2'), '__doc__'), hello.__doc__)
def test_ddt_data_doc_attribute(): """ Test the ``__doc__`` attribute handling of ``data`` items with ``ddt`` """ def func_w_doc(): """testFunctionDocstring {6} :param: None :return: None """ pass def func_wo_doc(): pass class Myint(int): pass class Mytest(object): pass d1 = Myint(1) d1.__name__ = 'case1' d1.__doc__ = """docstring1""" d2 = Myint(2) d2.__name__ = 'case2' data_hello = data(d1, d2, {'test': True})(func_w_doc) data_hello2 = data(d1, d2, {'test': True})(func_wo_doc) setattr(Mytest, 'first_test', data_hello) setattr(Mytest, 'second_test', data_hello2) ddt_mytest = ddt(Mytest) assert_equal(getattr(getattr(ddt_mytest, 'first_test_1_case1'), '__doc__'), d1.__doc__) assert_equal(getattr(getattr(ddt_mytest, 'first_test_2_case2'), '__doc__'), func_w_doc.__doc__) assert_equal(getattr(getattr(ddt_mytest, 'first_test_3'), '__doc__'), func_w_doc.__doc__) assert_equal( getattr(getattr(ddt_mytest, 'second_test_1_case1'), '__doc__'), d1.__doc__) assert_equal( getattr(getattr(ddt_mytest, 'second_test_2_case2'), '__doc__'), None) assert_equal(getattr(getattr(ddt_mytest, 'second_test_3'), '__doc__'), None)
def build_suite(self, suite: models.Suite) -> unittest.TestSuite: """组装suite""" class TestClass(unittest.TestCase): @classmethod def setUpClass(cls): self._register_suite(suite) logging.info('运行Suite', suite) for index, case in enumerate(suite._cases): test_method = self.build_case(index, case) setattr(TestClass, f'test_method_{index + 1}', test_method) TestClass = ddt.ddt(TestClass) TestClass.__doc__ = suite._name # suite名 test_suite = unittest.defaultTestLoader.loadTestsFromTestCase( TestClass) return test_suite
def create(self): """Main method for generating of test class based on parameters specified in a test scenario. Returns: BaseTest: Generated test class """ if not self._validate_scenario(): return self.test_class if not self._validate_drivers(): return self.test_class if self._check_skip(): return self.test_class if self._check_data(): return ddt.ddt(self.test_class) return self.test_class
def test_ddt_data_doc_attribute(): """ Test the ``__doc__`` attribute handling of ``data`` items with ``ddt`` """ def func_w_doc(): """testFunctionDocstring {6} :param: None :return: None """ pass def func_wo_doc(): pass class Myint(int): pass class Mytest(object): pass d1 = Myint(1) d1.__name__ = 'case1' d1.__doc__ = """docstring1""" d2 = Myint(2) d2.__name__ = 'case2' data_hello = data(d1, d2, {'test': True})(func_w_doc) data_hello2 = data(d1, d2, {'test': True})(func_wo_doc) setattr(Mytest, 'first_test', data_hello) setattr(Mytest, 'second_test', data_hello2) ddt_mytest = ddt(Mytest) assert_equal( getattr( getattr(ddt_mytest, 'first_test_1_case1'), '__doc__'), d1.__doc__ ) assert_equal( getattr( getattr(ddt_mytest, 'first_test_2_case2'), '__doc__'), func_w_doc.__doc__ ) assert_equal( getattr( getattr(ddt_mytest, 'first_test_3'), '__doc__'), func_w_doc.__doc__ ) assert_equal( getattr( getattr(ddt_mytest, 'second_test_1_case1'), '__doc__'), d1.__doc__ ) assert_equal( getattr( getattr(ddt_mytest, 'second_test_2_case2'), '__doc__'), None ) assert_equal( getattr( getattr(ddt_mytest, 'second_test_3'), '__doc__'), None )
def __new__(mcs, name, bases, attributes): """ Adds a create method for the specific class. @param mcs: @param name: @param bases: @param attributes: @return: """ _klass = super( MetaCLITest, mcs).__new__(mcs, name, bases, attributes) # When loading test classes for a test run, the Nose class # loader "transplants" any class that inherits from unittest.TestCase # into an internal class "C". If your test class uses MetaCLI, # then it will automatically also inherit from BaseCLI and # Nose will automatically see a new "C". We want to ignore # this class when using MetaCLITest if name == 'C': return _klass # Only perform attribute tests if instance is MetaCLITest parents = [b for b in bases if isinstance(b, MetaCLITest)] if not parents: return _klass # Make sure test module has required properties if not hasattr(_klass, "factory"): raise AttributeError("No 'factory' attribute found.") if not hasattr(_klass, "factory_obj"): raise AttributeError("No 'factory_obj' attribute found.") if not hasattr(_klass, "search_key"): setattr(_klass, 'search_key', 'name') # If the factory is a "plain" function makes it a staticmethod if isinstance(attributes['factory'], types.FunctionType): setattr(_klass, 'factory', staticmethod(attributes['factory'])) for name in NAMES: test_name = 'test_%s' % name if not test_name in attributes.keys(): data_name = '%s_data' % name # The data provided is a tuple so we need to unpack to pass to # the data decorator e.g. @data(*((a, b), (a, c))) if data_name.upper() in attributes.keys(): # Use data provided by test class params = attributes[data_name.upper()] else: # Use data provided by default_data module params = getattr(default_data, data_name.upper()) # Pass data to @data decorator func = data(*params)(getattr(template_methods, test_name)) # Update method's docstring to include name of object func.__doc__ = func.__doc__.replace( 'FOREMAN_OBJECT', _klass.factory_obj.__name__) # Add method to test class setattr(_klass, test_name, func) # Apply ddt decorator to class _klass = ddt(_klass) return _klass
def __new__(cls, name, bases, attributes): """ Adds a create method for the specific class. @param mcs: @param name: @param bases: @param attributes: @return: """ _klass = super(MetaCLITest, cls).__new__(cls, name, bases, attributes) # When loading test classes for a test run, the Nose class # loader "transplants" any class that inherits from unittest.TestCase # into an internal class "C". If your test class uses MetaCLI, # then it will automatically also inherit from BaseCLI and # Nose will automatically see a new "C". We want to ignore # this class when using MetaCLITest if name == 'C': return _klass # Only perform attribute tests if instance is MetaCLITest parents = [b for b in bases if isinstance(b, MetaCLITest)] if not parents: return _klass # Make sure test module has required properties if not hasattr(_klass, 'factory'): raise AttributeError('No "factory" attribute found.') if not hasattr(_klass, 'factory_obj'): raise AttributeError('No "factory_obj" attribute found.') if not hasattr(_klass, 'search_key'): setattr(_klass, 'search_key', 'name') # If the factory is a "plain" function makes it a staticmethod if isinstance(attributes['factory'], types.FunctionType): setattr(_klass, 'factory', staticmethod(attributes['factory'])) for name in NAMES: test_name = 'test_%s' % name if test_name not in attributes.keys(): data_name = '%s_data' % name # The data provided is a tuple so we need to unpack to pass to # the data decorator e.g. @data(*((a, b), (a, c))) if data_name.upper() in attributes.keys(): # Use data provided by test class params = attributes[data_name.upper()] else: # Use data provided by default_data module params = getattr(default_data, data_name.upper()) # Pass data to @data decorator func = data(*params)(getattr(template_methods, test_name)) # Update method's docstring to include name of object func.__doc__ = func.__doc__.replace( 'FOREMAN_OBJECT', _klass.factory_obj.__name__) # Add method to test class setattr(_klass, test_name, func) # Apply ddt decorator to class _klass = ddt(_klass) return _klass