如何为每个单元测试定义 class 个实例?
How to define class instances per unit test?
我有以下class,我想为它写几个单元测试:
class JsonSchemaValidator:
def __init__(self, json_file):
self.json_file = json_file
self.schema = json.load(open(json_file))
def check_json_schema(self):
print(self.json_file)
Draft3Validator.check_schema(self.schema)
如上所示,class 有两个 self.json_file
和 self.schema
实例,我想为每个测试定义架构。我如何设置测试,以便可以为每个测试用例定义模式?
class TestValidator(TestCase):
def test_check_json_schema1(self):
schema = {
"type": "object",
"properties": {
"key1": {"type": "string"}
},
}
actual = JsonSchemaValidator.check_json_schema() ##??
self.assertIsNone(actual)
def test_check_json_schema2(self):
schema = {
"type": "object",
"properties": {
"key2": {"type": "SOME_TYPE"}
},
}
self.assertRaises(SchemaError, JsonSchemaValidator.check_json_schema, schema) ##??
问题是您不希望您的代码实际上 open
磁盘上的一个文件并且 load
它,您只想提供结果。
一种方法是 mock open
和 json.load
引用 TestValidator
使用,就像那样:
import json
import unittest
import unittest.mock as mocking
class JsonSchemaValidator:
def __init__(self, json_file_path):
self.json_file_path = json_file_path
self.schema = json.load(open(json_file_path))
def check(self):
print(self.json_file_path)
# do the validation here
class TestValidator(unittest.TestCase):
def test_check_json_schema1(self):
schema = {
"type": "object",
"properties": {
"key1": {"type": "string"}
},
}
with mocking.patch("builtins.open"), \
mocking.patch.object(json, "load", new=mocking.Mock(return_value=schema)):
validator = JsonSchemaValidator("/blah")
print(validator.schema) # exactly what we wanted : {'type': 'object', 'properties': {'key1': {'type': 'string'}}}
# do your test here
validator.check()
...
你可以通过将 print(json.load, open)
添加到 JsonSchemaValidator.__init__
来检查,你会得到类似的东西:
<Mock id='139724038827840'> <MagicMock name='open' id='139724039146992'>
因为当您在上下文管理器中时他们被嘲笑了 (with
)。
(我将 json_file
重命名为 json_file_path
因为我认为这样更清楚)
我有以下class,我想为它写几个单元测试:
class JsonSchemaValidator:
def __init__(self, json_file):
self.json_file = json_file
self.schema = json.load(open(json_file))
def check_json_schema(self):
print(self.json_file)
Draft3Validator.check_schema(self.schema)
如上所示,class 有两个 self.json_file
和 self.schema
实例,我想为每个测试定义架构。我如何设置测试,以便可以为每个测试用例定义模式?
class TestValidator(TestCase):
def test_check_json_schema1(self):
schema = {
"type": "object",
"properties": {
"key1": {"type": "string"}
},
}
actual = JsonSchemaValidator.check_json_schema() ##??
self.assertIsNone(actual)
def test_check_json_schema2(self):
schema = {
"type": "object",
"properties": {
"key2": {"type": "SOME_TYPE"}
},
}
self.assertRaises(SchemaError, JsonSchemaValidator.check_json_schema, schema) ##??
问题是您不希望您的代码实际上 open
磁盘上的一个文件并且 load
它,您只想提供结果。
一种方法是 mock open
和 json.load
引用 TestValidator
使用,就像那样:
import json
import unittest
import unittest.mock as mocking
class JsonSchemaValidator:
def __init__(self, json_file_path):
self.json_file_path = json_file_path
self.schema = json.load(open(json_file_path))
def check(self):
print(self.json_file_path)
# do the validation here
class TestValidator(unittest.TestCase):
def test_check_json_schema1(self):
schema = {
"type": "object",
"properties": {
"key1": {"type": "string"}
},
}
with mocking.patch("builtins.open"), \
mocking.patch.object(json, "load", new=mocking.Mock(return_value=schema)):
validator = JsonSchemaValidator("/blah")
print(validator.schema) # exactly what we wanted : {'type': 'object', 'properties': {'key1': {'type': 'string'}}}
# do your test here
validator.check()
...
你可以通过将 print(json.load, open)
添加到 JsonSchemaValidator.__init__
来检查,你会得到类似的东西:
<Mock id='139724038827840'> <MagicMock name='open' id='139724039146992'>
因为当您在上下文管理器中时他们被嘲笑了 (with
)。
(我将 json_file
重命名为 json_file_path
因为我认为这样更清楚)