self.assertEqual
时间: 2023-08-29 17:08:26 浏览: 99
`self.assertEqual` 是 `unittest.TestCase` 类中的一个断言方法。它用于断言两个值是否相等。如果两个值不相等,则该断言会抛出一个 AssertionError 异常。
`self.assertEqual` 方法的语法如下:
```python
self.assertEqual(first, second, msg=None)
```
其中,`first` 和 `second` 是需要比较的两个值,`msg` 是可选参数,用于在断言失败时输出错误信息。
例如,以下代码使用 `self.assertEqual` 断言方法比较两个字符串:
```python
import unittest
class TestStringMethods(unittest.TestCase):
def test_upper(self):
self.assertEqual('hello'.upper(), 'HELLO')
```
在这个示例中,`self.assertEqual('hello'.upper(), 'HELLO')` 断言会比较 `'hello'.upper()` 和 `'HELLO'` 两个字符串是否相等,如果不相等,就会抛出一个 AssertionError 异常。
相关问题
self.assertequal断言写法
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.
阅读全文