测试是否由于密码错误引发了 sqlalchemy 异常 ID
Test if sqlalchemy exception id raised due to wrong password
我尝试为 sqlalchemy(mssql+pyodbc 引擎)连接编写一个测试用例,我有以下功能
def test_wrong_password(self):
self.config["DB_Connection"]["password"] = "sdkfajsdklj" #set wrong password
db = db_interaction(self.config["DB_Connection"])
with pytest.raises(sqlalchemy.exc.DBAPIError):
db.getusername("max.mustermann@example.com")
数据库连接的配置存储在配置字典中。在第一条语句中,我将密码更改为一些随机字符串。在第二条语句中,我用错误的密码初始化 db_interaction
的实例。
如果我 运行 使用错误的用户密码 xxx
程序,我会得到以下错误:
sqlalchemy.exc.DBAPIError: (Error) ('28000', "[28000] [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'xxx'. (18456) (SQLDriverConnect)") None None
我现在想测试一下,如果连接被拒绝是因为密码错误而不是因为任何其他异常。
我将如何实现这一目标?
看来你需要用这种代码检查异常:
with pytest.raises(DBAPIError) as excinfo:
# code that raises the exception
# code withing 'with' block will fail the test if exception didn't happen at all
# code below will additionally check whether exception was really due to failed login
# please note that it's outside of 'with' block
assert 'Login failed' in excinfo.value
查看:
我尝试为 sqlalchemy(mssql+pyodbc 引擎)连接编写一个测试用例,我有以下功能
def test_wrong_password(self):
self.config["DB_Connection"]["password"] = "sdkfajsdklj" #set wrong password
db = db_interaction(self.config["DB_Connection"])
with pytest.raises(sqlalchemy.exc.DBAPIError):
db.getusername("max.mustermann@example.com")
数据库连接的配置存储在配置字典中。在第一条语句中,我将密码更改为一些随机字符串。在第二条语句中,我用错误的密码初始化 db_interaction
的实例。
如果我 运行 使用错误的用户密码 xxx
程序,我会得到以下错误:
sqlalchemy.exc.DBAPIError: (Error) ('28000', "[28000] [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'xxx'. (18456) (SQLDriverConnect)") None None
我现在想测试一下,如果连接被拒绝是因为密码错误而不是因为任何其他异常。 我将如何实现这一目标?
看来你需要用这种代码检查异常:
with pytest.raises(DBAPIError) as excinfo:
# code that raises the exception
# code withing 'with' block will fail the test if exception didn't happen at all
# code below will additionally check whether exception was really due to failed login
# please note that it's outside of 'with' block
assert 'Login failed' in excinfo.value
查看: