UnitTests: Setup an environment for every test run

So there are no old file an temporary file.

Change-Id: Ida22703b5842b094f9a0f26562cd0908542e044f
Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io>
This commit is contained in:
Marco Bubke
2020-11-10 12:12:15 +01:00
parent f90bc7fa37
commit 2ee63af4cd

View File

@@ -36,13 +36,23 @@
#include <benchmark/benchmark.h>
#endif
class Environment : public testing::Environment
{
public:
void SetUp() override
{
const QString temporayDirectoryPath = QDir::tempPath() + "/QtCreator-UnitTests-XXXXXX";
Utils::TemporaryDirectory::setMasterTemporaryDirectory(temporayDirectoryPath);
qputenv("TMPDIR", Utils::TemporaryDirectory::masterDirectoryPath().toUtf8());
qputenv("TEMP", Utils::TemporaryDirectory::masterDirectoryPath().toUtf8());
}
void TearDown() override {}
};
int main(int argc, char *argv[])
{
Sqlite::Database::activateLogging();
const QString temporayDirectoryPath = QDir::tempPath() +"/QtCreator-UnitTests-XXXXXX";
Utils::TemporaryDirectory::setMasterTemporaryDirectory(temporayDirectoryPath);
qputenv("TMPDIR", Utils::TemporaryDirectory::masterDirectoryPath().toUtf8());
qputenv("TEMP", Utils::TemporaryDirectory::masterDirectoryPath().toUtf8());
QCoreApplication application(argc, argv);
@@ -51,6 +61,9 @@ int main(int argc, char *argv[])
benchmark::Initialize(&argc, argv);
#endif
Environment environment;
testing::AddGlobalTestEnvironment(&environment);
int testsHaveErrors = RUN_ALL_TESTS();
#ifdef WITH_BENCHMARKS