Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何编写自定义断言Python_Python_Unit Testing - Fatal编程技术网

如何编写自定义断言Python

如何编写自定义断言Python,python,unit-testing,Python,Unit Testing,我计划根据它测试的代码部分,将我的一个大测试文件拆分为更小的测试。我有一个自定义的assert函数用于我的一个测试。如果我将它们拆分为一个单独的文件,我应该如何将它们导入到其他测试文件中 TestSchemacase: class TestSchemaCase(unittest.TestCase): """ This will test our schema against the JSONTransformer output just to make sure the

我计划根据它测试的代码部分,将我的一个大测试文件拆分为更小的测试。我有一个自定义的assert函数用于我的一个测试。如果我将它们拆分为一个单独的文件,我应该如何将它们导入到其他测试文件中

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.我们应该这样继承他们吗? 类TestResunittest.TestCase,TestSchemaCase:

谢谢你抽出时间。很抱歉发了这么大的邮件


我确实看到了这篇文章,但这并不能解决问题。

我建议使用一个测试框架,不要强迫你将测试放在类中,比如。

可能的重复