2017-03-17 163 views
0

我打算将我的一个大测试文件拆分为基于它测试的代码部分的较小测试。我有一个自定义断言函数用于我的一个测试。如果我将它们分割成单独的文件,我应该如何将它们导入其他测试文件。如何编写自定义断言Python

TestSchemacase:

class TestSchemaCase(unittest.TestCase): 
    """ 
    This will test our schema against the JSONTransformer output 
    just to make sure the schema matches the model 
    """ 
    # pylint: disable=too-many-public-methods 


    _base_dir = os.path.realpath(os.path.dirname(__file__)) 
    def assertJSONValidates(self, schema, data): 
     """ 
     This function asserts the validation works as expected 

     Args: 
      schema(dict): The schema to test against 
      data(dict): The data to validate using the schema 
     """ 
     # pylint: disable=invalid-name 
     validator = jsonschema.Draft4Validator(schema) 
     self.assertIsNone(validator.validate(data)) 


    def assertJSONValidateFails(self, schema, data): 
     """ 
     This function will assertRaises an ValidationError Exception 
     is raised. 

     Args: 
      schema(dict): The schema to validate from 
      data(dict): The data to validate using the schema 
     """ 
     # pylint: disable=invalid-name 
     validator = jsonschema.Draft4Validator(schema) 
     with self.assertRaises(jsonschema.ValidationError): 
      validator.validate(data) 

我的问题是,1。当我尝试导入它们时,我得到一个导入错误,发现没有模块名称。我打破了TestValidation提到的小文件。 2.我知道我可以在assertJSONValidateFails中引发验证错误,但是如果验证通过,我应该返回什么。

tests/schema 
├── TestSchemaCase.py 
├── TestValidation.py 
├── __init__.py 
└── models 
    ├── Fields 
    │   ├── TestImplemen.py 
    │   ├── TestRes.py 
    │   └── __init__.py 
    ├── Values 
    │   ├── TestInk.py 
    │   ├── TestAlue.py 
    │   └── __init__.py 
    └── __init__.py 

3.我们该如何继承它们? class TestRes(unittest.TestCase,TestSchemaCase):

谢谢你的时间。对不起,大帖子

我没看到帖子,但那并没有解决问题。

+2

[如何在Python中编写自定义的'.assertFoo()''方法?](http://stackoverflow.com/questions/6655724/how-to-write-a-custom-assertfoo-方法合蟒) –

回答

1

我会建议使用不强制你把你的测试放在类中的测试框架,如pytest