self.assertequal断言写法
时间: 2024-01-16 15:04:23 浏览: 167
self.assertEqual是Python中unittest模块中的一个断言方法,用于判断两个值是否相等。其基本语法格式为:
```python
self.assertEqual(first, second, msg=None)
```
其中,first和second是需要比较的两个值,msg是可选参数,用于在断言失败时输出错误信息。
例如,判断两个整数是否相等:
```python
import unittest
class TestDemo(unittest.TestCase):
def test_equal(self):
self.assertEqual(1+2, 3)
if __name__ == '__main__':
unittest.main()
```
输出结果为:
```
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
```
相关问题
class Dog6: def __init__(self, initial_name): self._name = initial_name def get_self(self): return self def __str__(self): # # Implement this! # return __ def __repr__(self): return "<Dog named '" + self._name + "'>" def test_inside_a_method_self_refers_to_the_containing_object(self): fido = self.Dog6("Fido") self.assertEqual(__, fido.get_self()) # Not a string! def test_str_provides_a_string_version_of_the_object(self): fido = self.Dog6("Fido") self.assertEqual("Fido", str(fido)) def test_str_is_used_explicitly_in_string_interpolation(self): fido = self.Dog6("Fido") self.assertEqual(__, "My dog is " + str(fido)) def test_repr_provides_a_more_complete_string_version(self): fido = self.Dog6("Fido") self.assertEqual(__, repr(fido)) def test_all_objects_support_str_and_repr(self): seq = [1, 2, 3] self.assertEqual(__, str(seq)) self.assertEqual(__, repr(seq)) self.assertEqual(__, str("STRING")) self.assertEqual(__, repr("STRING"))
Here is the completed code with the missing parts:
```
class Dog6:
def __init__(self, initial_name):
self._name = initial_name
def get_self(self):
return self
def __str__(self):
return self._name
def __repr__(self):
return "<Dog named '" + self._name + "'>"
def test_inside_a_method_self_refers_to_the_containing_object(self):
fido = Dog6("Fido")
self.assertEqual(fido, fido.get_self()) # Not a string!
def test_str_provides_a_string_version_of_the_object(self):
fido = Dog6("Fido")
self.assertEqual("Fido", str(fido))
def test_str_is_used_explicitly_in_string_interpolation(self):
fido = Dog6("Fido")
self.assertEqual("My dog is Fido", "My dog is " + str(fido))
def test_repr_provides_a_more_complete_string_version(self):
fido = Dog6("Fido")
self.assertEqual("<Dog named 'Fido'>", repr(fido))
def test_all_objects_support_str_and_repr(self):
seq = [1, 2, 3]
self.assertEqual("[1, 2, 3]", str(seq))
self.assertEqual("[1, 2, 3]", repr(seq))
self.assertEqual("STRING", str("STRING"))
self.assertEqual("'STRING'", repr("STRING"))
```
Note that in `test_str_is_used_explicitly_in_string_interpolation`, the expected output is `"My dog is Fido"` instead of `"My dog is <Dog named 'Fido'>"` because the `+` operator implicitly calls `str` on the object being concatenated.
# Add a new method to an existing class. def test_after_patching_dogs_can_both_wag_and_bark(self): def wag(self): return "HAPPY" self.Dog.wag = wag fido = self.Dog() self.assertEqual('HAPPY', fido.wag()) self.assertEqual('WOOF', fido.bark())
这是一个关于如何给一个已有的类添加新方法的 Python 测试代码。这段代码定义了一个名为 `test_after_patching_dogs_can_both_wag_and_bark` 的测试方法,该方法使用了 Python 的内置 `assertEqual` 函数来检查给狗类添加了 `wag` 方法后,狗实例是否可以成功调用该方法并返回正确的结果;同时,该方法也检查了狗实例是否可以成功调用原有的 `bark` 方法并返回正确的结果。
阅读全文