diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp
index 46430133719b414f9b4d54164f7fdbe5b15d479d..6e30a5d429b7997fefb853d6fa14498d6d79f24d 100644
--- a/src/testlib/qtestcase.cpp
+++ b/src/testlib/qtestcase.cpp
@@ -1455,6 +1455,7 @@ namespace QTest
     static int keyDelay = -1;
     static int mouseDelay = -1;
     static int eventDelay = -1;
+    static int timeout = -1;
     static bool noCrashHandler = false;
 
 /*! \internal
@@ -1506,6 +1507,18 @@ int Q_TESTLIB_EXPORT defaultKeyDelay()
     return keyDelay;
 }
 
+static int defaultTimeout()
+{
+    if (timeout == -1) {
+        bool ok = false;
+        timeout = qEnvironmentVariableIntValue("QTEST_FUNCTION_TIMEOUT", &ok);
+
+        if (!ok || timeout <= 0)
+            timeout = 5*60*1000;
+    }
+    return timeout;
+}
+
 static bool isValidSlot(const QMetaMethod &sl)
 {
     if (sl.access() != QMetaMethod::Private || sl.parameterCount() != 0
@@ -2119,7 +2132,7 @@ public:
 
     void beginTest() {
         QMutexLocker locker(&mutex);
-        timeout.store(5*60*1000);
+        timeout.store(defaultTimeout());
         waitCondition.wakeAll();
     }