Python 单元测试,发生错误并匹配异常,但我的程序 returns 一个 `AssertionError`
Python unit testing, error happened and exception matched, but my program returns an `AssertionError`
这是我的 Python 代码。
try: ct("table_that_does_not_exist", "database_that_does_not_exists")
except r.errors.ReqlOpFailedError as e: self.fail()
ct
是检查数据库中是否存在 table 的函数。 ct
returns True
如果数据库中存在 table,否则 False
。
try: ct("table_that_does_not_exist", "database_that_does_not_exists")
正在尝试检查不存在的数据库中不存在的 table。这应该 return 是一个错误。
rethinkdb.errors.ReqlOpFailedError: Database `database_that_does_not_exists` does not exist in:
r.db('database_that_does_not_exists').table_list().contains('table_that_does_not_exist')
但是,在 except r.errors.ReqlOpFailedError as e: self.fail()
和 returns 和 AssertionError
.
中有些错误未被捕获
我希望它能通过测试,因为 ct("table_that_does_not_exist", "database_that_does_not_exists")
将 return r.errors.ReqlOpFailedError
。但我得到了这个。
======================================================================
FAIL: test_check_table (__main__.test)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test.py", line 61, in test_check_table
try: ct("table_that_does_not_exist", "database_that_does_not_exists")
rethinkdb.errors.ReqlOpFailedError: Database `database_that_does_not_exists` does not exist in:
r.db('database_that_does_not_exists').table_list().contains('table_that_does_not_exist')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "test.py", line 62, in test_check_table
except r.errors.ReqlOpFailedError as e: self.fail()
AssertionError
----------------------------------------------------------------------
Ran 7 tests in 0.050s
FAILED (failures=1)
错误匹配但未继续通过 except
。
我的测试有什么问题?
我认为您对此的一般做法是不正确的。
您的异常检查代码应如下所示:
with self.assertRaises(r.errors.ReqlOpFailedError):
ct("table_that_does_not_exist", "database_that_does_not_exists")
这是我的 Python 代码。
try: ct("table_that_does_not_exist", "database_that_does_not_exists")
except r.errors.ReqlOpFailedError as e: self.fail()
ct
是检查数据库中是否存在 table 的函数。 ct
returns True
如果数据库中存在 table,否则 False
。
try: ct("table_that_does_not_exist", "database_that_does_not_exists")
正在尝试检查不存在的数据库中不存在的 table。这应该 return 是一个错误。
rethinkdb.errors.ReqlOpFailedError: Database `database_that_does_not_exists` does not exist in:
r.db('database_that_does_not_exists').table_list().contains('table_that_does_not_exist')
但是,在 except r.errors.ReqlOpFailedError as e: self.fail()
和 returns 和 AssertionError
.
我希望它能通过测试,因为 ct("table_that_does_not_exist", "database_that_does_not_exists")
将 return r.errors.ReqlOpFailedError
。但我得到了这个。
======================================================================
FAIL: test_check_table (__main__.test)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test.py", line 61, in test_check_table
try: ct("table_that_does_not_exist", "database_that_does_not_exists")
rethinkdb.errors.ReqlOpFailedError: Database `database_that_does_not_exists` does not exist in:
r.db('database_that_does_not_exists').table_list().contains('table_that_does_not_exist')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "test.py", line 62, in test_check_table
except r.errors.ReqlOpFailedError as e: self.fail()
AssertionError
----------------------------------------------------------------------
Ran 7 tests in 0.050s
FAILED (failures=1)
错误匹配但未继续通过 except
。
我的测试有什么问题?
我认为您对此的一般做法是不正确的。 您的异常检查代码应如下所示:
with self.assertRaises(r.errors.ReqlOpFailedError):
ct("table_that_does_not_exist", "database_that_does_not_exists")