PySide6.QtTest.QTest

class QTest

QTest 命名空间包含所有与 Qt 测试相关的函数和声明。更多

概要

静态函数

注意

本文档可能包含从C++自动翻译到Python的代码片段。我们始终欢迎对代码片段翻译的贡献。如果您发现翻译问题,您也可以通过在我们的https:/bugreports.qt.io/projects/PYSIDE上创建工单来告知我们。

详细描述

查看Qt 测试概述以获取有关如何编写单元测试的信息。

class TestFailMode

此枚举描述了处理检查的模式,例如通过QVERIFY()QCOMPARE()宏,这些宏已知会失败。无论检查失败还是成功,该模式都适用。

常量

描述

QTest.Abort

中止测试的执行。当在有问题的检查后继续执行测试没有意义时,使用此模式。

QTest.Continue

在问题检查后继续执行测试。

另请参阅

QEXPECT_FAIL()

class ComparisonOperation

在版本6.4中添加。

class QBenchmarkMetric

此枚举列出了所有可以进行基准测试的内容。

常量

描述

QTest.FramesPerSecond

每秒帧数

QTest.BitsPerSecond

每秒比特数

QTest.BytesPerSecond

每秒字节数

QTest.WalltimeMilliseconds

以毫秒为单位的时钟时间

QTest.WalltimeNanoseconds

时钟时间,以纳秒为单位

QTest.BytesAllocated

内存使用量(字节)

QTest.Events

事件计数

QTest.CPUTicks

CPU时间

QTest.CPUMigrations

CPU之间的进程迁移

QTest.CPUCycles

CPU周期

QTest.RefCPUCycles

参考CPU周期

QTest.BusCycles

总线周期

QTest.StalledCycles

停滞周期

QTest.InstructionReads

指令读取

QTest.Instructions

已执行的指令

QTest.BranchInstructions

分支类型指令

QTest.BranchMisses

错误预测的分支指令

QTest.CacheReferences

缓存任何类型的访问

QTest.CacheMisses

任何类型的缓存未命中

QTest.CacheReads

缓存读取 / 加载

QTest.CacheReadMisses

缓存读取/加载未命中

QTest.CacheWrites

缓存写入 / 存储

QTest.CacheWriteMisses

缓存写入/存储未命中

QTest.CachePrefetches

缓存预取

QTest.CachePrefetchMisses

缓存预取未命中

QTest.ContextSwitches

上下文切换

QTest.PageFaults

任何类型的页面错误

QTest.MinorPageFaults

次要页面错误

QTest.MajorPageFaults

主要页面错误

QTest.AlignmentFaults

由于未对齐导致的故障

QTest.EmulationFaults

需要软件模拟的故障

请注意,WalltimeNanosecondsBytesAllocated 仅用于通过 setBenchmarkResult() 使用,并且这些指标的结果无法由 QTest 框架自动提供。

另请参阅

benchmarkMetricName() benchmarkMetricUnit()

在版本4.7中添加。

static addColumnInternal(id, name)
Parameters:
  • id – int

  • name – str

static asciiToKey(ascii)
Parameters:

ascii – 整数

Return type:

Key

static compare_ptr_helper(t1, t2, actual, expected, file, line)
Parameters:
  • t1QObject

  • t2QObject

  • actual – str

  • expected – str

  • file – str

  • line – int

Return type:

布尔

static compare_ptr_helper(t1, t2, actual, expected, file, line)
Parameters:
  • t1void

  • t2void

  • actual – str

  • expected – str

  • file – str

  • line – int

Return type:

布尔

static compare_string_helper(t1, t2, actual, expected, file, line)
Parameters:
  • t1 – str

  • t2 – 字符串

  • actual – str

  • expected – str

  • file – str

  • line – int

Return type:

布尔

static currentAppName()
Return type:

字符串

返回当前执行的二进制文件的名称。

static currentDataTag()
Return type:

字符串

返回当前测试数据的名称。如果测试没有分配任何测试数据,函数将返回None

static currentTestFailed()
Return type:

布尔

如果当前测试函数失败,则返回true,否则返回false。

另请参阅

currentTestResolved()

static currentTestFunction()
Return type:

字符串

警告

本节包含从C++自动翻译到Python的代码片段,可能包含错误。

返回当前执行的测试函数的名称。

示例:

def cleanup(self):

    if qstrcmp(QTest.currentTestFunction(), "myDatabaseTest") == 0:
        # clean up all database connections
        closeAllDatabases()
static currentTestResolved()
Return type:

布尔

如果当前测试函数失败或跳过,则返回true

如果测试失败或执行了跳过操作,则适用此情况。当为true时,测试函数应提前返回。特别是,如果在测试函数(但不是其cleanup())期间执行了QTRY_*宏和测试事件循环,它们会提前终止循环。在测试调用了使用此模块宏的辅助函数后,它可以使用此函数来测试是否应提前返回。

另请参阅

currentTestFailed()

static failOnWarning()

此函数重载了 failOnWarning()。

如果输出了任何警告,则将测试失败附加到测试日志中。

另请参阅

failOnWarning()

static failOnWarning(messagePattern)
Parameters:

messagePatternQRegularExpression

对于每个与messagePattern匹配的警告,将测试失败附加到测试日志中。

测试函数在添加失败时会继续执行。要中止测试,您可以检查currentTestFailed(),如果它是true,则提前返回。

对于每个警告,第一个匹配的模式将导致失败,其余的模式将被忽略。

所有模式在每个测试函数结束时都会被清除。

void FileTest::loadFiles()
{
    QTest::failOnWarning(QRegularExpression("^Failed to load"));

    // Each of these will cause a test failure:
    qWarning() << "Failed to load image";
    qWarning() << "Failed to load video";
}

要使每个触发给定警告的测试失败,请在init()中向此函数传递一个合适的正则表达式:

void FileTest::init()
{
    QTest::failOnWarning(
        QRegularExpression("QFile::.*: File(.*) already open"));
}

对于在任何警告下失败的常见情况,不传递任何参数:

void FileTest::init()
{
    QTest::failOnWarning();
}

注意

ignoreMessage() 优先于此函数,因此任何与 ignoreMessage()failOnWarning() 提供的模式匹配的警告都将被忽略。

另请参阅

QTEST_FATAL_FAIL

static failOnWarning(message)
Parameters:

消息 – str

此函数重载了 failOnWarning()

如果输出了message,则将测试失败追加到测试日志中。

另请参阅

failOnWarning()

static formatString(prefix, suffix, numArguments)
Parameters:
  • prefix – str

  • suffix – str

  • numArguments – 整数

Return type:

char

static ignoreMessage(type, messagePattern)
Parameters:

这是一个重载函数。

忽略由qDebug()、qInfo()或qWarning()创建的消息。如果与messagePattern匹配的消息和相应的type被输出,它将被从测试日志中移除。如果测试完成且消息未被输出,则会在测试日志中附加一个测试失败。

注意

调用此函数只会忽略一条消息。如果您想忽略的消息输出了两次,您也必须调用ignoreMessage()两次。

static ignoreMessage(type, message)
Parameters:

警告

本节包含从C++自动翻译到Python的代码片段,可能包含错误。

忽略由qDebug()、qInfo()或qWarning()创建的消息。如果输出了具有相应typemessage,它将被从测试日志中移除。如果测试完成且message未被输出,则会在测试日志中附加一个测试失败。

注意

调用此函数只会忽略一条消息。如果您想忽略的消息输出了两次,您也必须调用ignoreMessage()两次。

示例:

dir = QDir()
QTest.ignoreMessage(QtWarningMsg, "QDir.mkdir: Empty or null file name(s)")
dir.mkdir("")

上面的例子测试了当使用无效的文件名调用时,QDir::mkdir() 是否输出了正确的警告。

static keyToAscii(key)
Parameters:

Key

Return type:

整数

static qCaught(expected, file, line)
Parameters:
  • expected – str

  • file – str

  • line – int

static qCaught(expected, what, file, line)
Parameters:
  • expected – str

  • 什么 – str

  • file – str

  • line – int

static qCleanup()
static qElementData(elementName, metaTypeId)
Parameters:
  • elementName – str

  • metaTypeId – int

Return type:

void

static qExpectFail(dataIndex, comment, mode, file, line)
Parameters:
  • dataIndex – str

  • comment – str

  • modeTestFailMode

  • file – str

  • line – int

Return type:

布尔

static qExtractTestData(dirName)
Parameters:

dirName – str

Return type:

QSharedPointer

从资源中提取一个目录到磁盘。内容会递归地提取到一个临时文件夹中。一旦返回值的最后一个引用超出范围,提取的内容将自动删除。

dirName 是要从资源中提取的目录名称。

返回数据被提取到的临时目录,如果出现错误则返回null。

static qFindTestData(basepath[, file=None[, line=0[, builddir=None[, sourcedir=None]]]])
Parameters:
  • basepath – str

  • file – str

  • line – int

  • builddir – str

  • sourcedir – str

Return type:

字符串

static qFindTestData(basepath[, file=None[, line=0[, builddir=None[, sourcedir=None]]]])
Parameters:
  • basepath – str

  • file – str

  • line – int

  • builddir – str

  • sourcedir – str

Return type:

字符串

static qGlobalData(tagName, typeId)
Parameters:
  • tagName – str

  • typeId – int

Return type:

void

static qRun()
Return type:

整数

static qSkip(message, file, line)
Parameters:
  • message – str

  • file – str

  • line – int

static qSleep(ms)
Parameters:

ms – 整数

这是一个重载函数。

睡眠 ms 毫秒,阻塞测试的执行。

等同于调用:

QTest::qSleep(std::chrono::milliseconds{ms});
static qWait(ms)
Parameters:

ms – 整数

这是一个重载函数。

等待 msecs。相当于调用:

QTest::qWait(std::chrono::milliseconds{msecs});
static runningTest()
Return type:

布尔

static setBenchmarkResult(result, metric)
Parameters:

将此测试函数的基准结果设置为result

如果你想在不使用QBENCHMARK宏的情况下报告基准测试结果,请使用此函数。使用metric来指定Qt Test应如何解释结果。

结果的上下文将是测试函数名称和来自_data函数的任何数据标签。此函数在每个测试函数中只能调用一次,后续调用将替换先前报告的结果。

请注意,-iterations 命令行参数对没有使用 QBENCHMARK 宏的测试函数没有影响。

static setMainSourcePath(file[, builddir=None])
Parameters:
  • file – str

  • builddir – str

static setThrowOnFail(enable)
Parameters:

enable – 布尔值

启用(enable = true)或禁用(enable = false)在QCOMPARE() / QVERIFY()失败时抛出异常(而不是仅仅从直接包围的函数上下文中返回)。

该功能是引用计数的:如果你用true调用这个函数N次,你需要用false调用它N次才能回到起点。

默认是false,除非设置了QTEST_THROW_ON_FAIL环境变量

当定义了QTEST_THROW_ON_FAIL C++宏时,此调用无效。

注意

您必须启用异常编译您的测试才能使用此功能。

另请参阅

setThrowOnSkip() ThrowOnFailEnabler ThrowOnFailDisabler QTEST_THROW_ON_FAIL

static setThrowOnSkip(enable)
Parameters:

enable – 布尔值

启用(enable = true)或禁用(enable = false)在QSKIP()上抛出异常(而不是仅仅从直接包围的函数上下文中返回)。

该功能是引用计数的:如果你用true调用这个函数N次,你需要用false调用它N次才能回到起点。

默认是 false,除非设置了 QTEST_THROW_ON_SKIP 环境变量

当定义了QTEST_THROW_ON_SKIP C++宏时,此调用无效。

注意

您必须启用异常编译您的测试才能使用此功能。

另请参阅

setThrowOnFail() ThrowOnSkipEnabler ThrowOnSkipDisabler QTEST_THROW_ON_SKIP

static testObject()
Return type:

QObject

static toPrettyCString(unicode, length)
Parameters:
  • unicode – 字符串

  • length – int

Return type:

char