如何从Invoke-Pester获取失败测试的数目?

时间:2019-04-12 12:42:10

标签: powershell pester

我在控制台中可以正常运行一些Pester测试,但是我想自动运行测试,如果任何测试失败,则发送一条消息。我阅读了-EnableExit选项,导致Invoke-Pester返回失败测试的数目。但是无论何时我使用-EnableExit,PowerShell控制台都会关闭,无论测试是否失败。它是Pester版本4.7.3。 PSVersion 5.1。

Invoke-Pester -EnableExit是否应该关闭外壳?
如何获得失败的测试数量?

运行良好:
Invoke-Pester -Script D:\tmp\PowerShell\dummy1.Tests.ps1

关闭外壳窗口:
Invoke-Pester -Script D:\tmp\PowerShell\dummy1.Tests.ps1 -EnableExit

我希望得到一个整数作为输出,但是控制台窗口关闭。

3 个答案:

答案 0 :(得分:1)

您可以使用def create_table(self): self.curr.execute("""DROP TABLE IF EXISTS saleitems_hm""") self.curr.execute("""create table saleitems_hm( hm_title text, hm_regular_price text, hm_sale_price text, hm_photo_url text, hm_description_url text )""") self.curr.execute("""DROP TABLE IF EXISTS saleitems_f21""") self.curr.execute("""create table saleitems_f21( f21_title text, f21_regular_price text, f21_sale_price text, f21_photo_url text, f21_description_url text )""") def process_item(self, item, spider): self.store_db(item) return item def store_db(self, item): self.curr.execute("""insert into saleitems_hm values (%s, %s, %s, %s, %s)""", ( item['hm_title'], item['hm_regular_price'], item['hm_sale_price'], item['hm_photo_url'], item['hm_description_url'] )) self.conn.commit() self.curr.execute("""insert into saleitems_f21 values (%s, %s, %s, %s, %s)""", ( items_f21['f21_title'], items_f21['f21_regular_price'], items_f21['f21_sale_price'], items_f21['f21_photo_url'], items_f21['f21_description_url'] )) self.conn.commit() 上的-PassThru开关来获取失败测试的次数。例如:

Invoke-Pester

然后,我的$TestResults = Invoke-Pester -PassThru 变量具有$TestResults属性,其中包含失败测试的数量。然后,如果测试失败,则可以将其用作管道的一部分以使管道失败:

FailedCount

以下是If ($TestResults.FailedCount -gt 0) { Throw "There were $($TestResults.FailedCount) failed tests" } 返回的其他示例:

-PassThru

答案 1 :(得分:0)

使用-PassThru的{​​{1}}开关参数

Invoke-Pester

答案 2 :(得分:0)

您可以通过以下操作获得失败测试的数量:

(Invoke-Pester -Path D:\tmp\PowerShell\dummy1.Tests.ps1 -PassThru -Show None).FailedCount

如果您需要其他数据(通过/跳过的计数,测试结果等),则将输出传递给变量,然后进行进一步处理:

$testResults = Invoke-Pester -Path D:\tmp\PowerShell\dummy1.Tests.ps1 -PassThru -Show None

相关问题