我正在为Qt中的函数编写单元测试。
我的密码
我有一个函数要测试:
QSqlDatabase createDB() {
QString database = "library";
QString dbPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
QDir appDataDir(dbPath);
if (!appDataDir.exists()) {
appDataDir.mkpath(dbPath);
}
dbPath += "/" + database + ".sqlite";
QSqlDatabase db;
db.setDatabaseName(dbPath);
return db;
}所以在我的测试中:
void DatabaseManagerTest::initTestCase()
{
QStandardPaths::setTestModeEnabled(true);
}
void DatabaseManagerTest::testDb()
{
QSqlDatabase m_db = createDb();
QString database = "library";
QString databasePath_exp = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)
+ "/" + database + ".sqlite";
QCOMPARE(m_db.databaseName(), databasePath_exp);
qDebug() << m_db.databaseName();
}
QTEST_MAIN( DatabaseManagerTest )测试的输出是
1: FAIL! : DatabaseManagerTest::testDb() Compared values are not the same
1: Actual (m_db.databaseName()): "/home/olivier/.local/share/MyTest/library.sqlite"
1: Expected (databasePath_exp) : "/home/olivier/.qttest/share/MyTest/library.sqlite"问题
createDb()如何使用Qt的测试StandardPaths而不是真正的测试StandardPaths?
发布于 2016-02-20 17:05:29
测试不应该使用全局变量,而“QStandardPath::writableLocation”是全局变量的包装器。我认为,如果不注入这种依赖性,您就无法摆脱这种状况:您可以:
https://stackoverflow.com/questions/35525444
复制相似问题