我正在为一个项目编写一些测试用例。在我的项目中,“地址”对象必须包含3个属性集,即“名称”,“电子邮件”,“电话”或“地址”,“城市”,“ state_id”。
@observes('name', 'email', 'phone', 'address', 'city',
'states_id', 'state')
def validate_address_obj(self, name, email, phone, address, city,
states_id, state):
name_email_phone = name and email and phone
address_city_state = address and city and (states_id or state)
if not name_email_phone and not address_city_state:
raise AssertionError(
"A valid address has to have: {} AND/OR {} defined.".format(
"name, email, and phone", "address, city, and state"))
# fi
# end def validate_address_obj
此函数验证是否存在一组属性,如果不存在所需的属性,则应引发“ AssertionError”。当我运行测试时,不会引发任何异常。
def test_validate_addresses(address_factory):
""" An Address must consist of at least: a name, phone number, and email OR
an address, city, and state """
# Testing name, email, and phone need to be set
# when address, city, and states_id is not
with pytest.raises(AssertionError):
address_factory(name='', address='', city='', state=None)
with pytest.raises(AssertionError):
address_factory(email='', address='', city='', state=None)
with pytest.raises(AssertionError):
address_factory(phone='', address='', city='', state=None)
# Testing address, city, and states_id need to be set
# when name, email, and phone is not
with pytest.raises(AssertionError):
address_factory(address='', name='', email='', phone='')
with pytest.raises(AssertionError):
address_factory(city='', name='', email='', phone='')
with pytest.raises(AssertionError):
address_factory(state=None, name='', email='', phone='')
# end def test_validate_addresses
这是我的测试用例的一个片段。