- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我最近切换到 Pester 5.0.2,开始对我编写的 Powershell 脚本进行一些测试。我能够进入 BeforeAll block 并且路径正在按照我的预期填充,但是当我到达我的 Describe block 中的 Get-ChildItem 区域时,BeforeAll 中的变量为 $null。
BeforeAll {
$testDir = Split-Path $PSCommandPath -Parent
$prodDir = Split-Path $testDir -Parent
}
Describe "Tests - All Files in prod" {
Get-ChildItem $prodDir -File | ForEach-Object {
$fileName = (Split-Path $_ -leaf).PadRight(20, ' ')
$filePath = $_.PSPath
It "Vars not declared or changed between function definitions" {
[System.Collections.ArrayList]$preSource = (Get-Variable).Name
. "$filePath" -Sourcing
[System.Collections.ArrayList]$postSource = (Get-Variable).Name
$postSource.Remove('preSource')
$postSource.Remove('UnderTest')
[array]$diff = Compare-Object $preSource $postSource -PassThru
$diff | Should -BeNullOrEmpty
}
It "$fileName all #*, #?, #TODO tags are removed" {
$_ | Should -Not -FileContentMatch $([regex]::escape('#TODO'))
$_ | Should -Not -FileContentMatch $([regex]::escape('#*'))
$_ | Should -Not -FileContentMatch $([regex]::escape('#?'))
}
}
}
最佳答案
我找到了答案,但在文档中很难找到。由于新的“发现”功能,这不再有效。这在所有 Describe、It、Context、BeforeAll、AfterAll 等 block 之前运行。然后丢弃此处运行的代码。
我最终使用了使用 -TestCase 参数的测试用例来解决问题。
编辑:
下面的版本带有关于为什么它不起作用的评论,然后是如何设置它的示例。
BeforeAll {
# This block will run AFTER discovery. Anything you need to be evaluated in front of the tests should
# be placed here
$testDir = Split-Path $PSCommandPath -Parent
$prodDir = Split-Path $testDir -Parent
}
$iWillDisappearAfterDiscovery = 'Byebye'
Describe "Describing your tests" {
# Code placed here will be run DURING discovery, and will not be availble at runtime. Placing the code from
# BeforeAll here instead would not work as $testDir and $prodDir would be empty when the tests ran.
#
# The apparent exception to this rule is that the tests are done DURING discovery and then stashed for when
# the tests run. That means your test cases can be set up here if not done directly inside the -TestCases parameter
#
Get-ChildItem $prodDir -File | ForEach-Object {
$fileName = (Split-Path $_ -leaf).PadRight(20, ' ')
$filePath = $_.PSPath
It "Vars not declared or changed between function definitions" {
[System.Collections.ArrayList]$preSource = (Get-Variable).Name
. "$filePath" -Sourcing
[System.Collections.ArrayList]$postSource = (Get-Variable).Name
$postSource.Remove('preSource')
$postSource.Remove('UnderTest')
[array]$diff = Compare-Object $preSource $postSource -PassThru
$diff | Should -BeNullOrEmpty
}
It "$fileName all #*, #?, #TODO tags are removed" {
$_ | Should -Not -FileContentMatch $([regex]::escape('#TODO'))
$_ | Should -Not -FileContentMatch $([regex]::escape('#*'))
$_ | Should -Not -FileContentMatch $([regex]::escape('#?'))
}
}
}
# Here's a correct example:
BeforeAll {
$testDir = Split-Path $PSCommandPath -Parent
$prodDir = Split-Path $testDir -Parent
}
Describe "Describing your tests" {
# You can still set them up dynamically as test cases
[System.Collections.ArrayList]$testCases = @()
Get-ChildItem $prodDir -File | ForEach-Object {
$fileName = (Split-Path $_ -leaf).PadRight(20, ' ')
$filePath = $_.PSPath
$testCases.Add({fileName = $fileName; filePath = $filePath})
}
It "<fileName> Exists" -TestCases $testCases {
#using <varName> will dynamically name your tests with data
# from your test cases
$filePath | Should -Exist
}
It "<fileName> all #*, #?, #TODO tags are removed" {
$filePath | Should -Not -FileContentMatch $([regex]::escape('#TODO'))
$filePath | Should -Not -FileContentMatch $([regex]::escape('#*'))
$filePath | Should -Not -FileContentMatch $([regex]::escape('#?'))
}
Context "Different Context, Run different tests" {
$testCases = @{filename = 'file4'.PadRight(20, ' '); filepath = '/path/to/file4.ps1' },
@{filename = 'file5'.PadRight(20, ' '); filepath = '/path/to/file5.ps1' }
It "Exists" {
$filePath | Should -Exist
}
}
Context "You can also set them up inline like this" -TestCases @(
$testCases = @{filename = 'file1'.PadRight(20, ' '); filepath = '/path/to/file1.ps1' },
@{filename = 'file2'.PadRight(20, ' '); filepath = '/path/to/file2.ps1' },
@{filename = 'file3'.PadRight(20, ' '); filepath = '/path/to/file3.ps1' }
) {
It "Run some tests" {
$fileName | Should -BeOfType [string]
}
}
}
关于powershell - Pester 5.0.2 BeforeAll block 代码未显示在 Describe block 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62497134/
我看过很多ReactJS中测试的例子都用it() 示例: describe('General behaviour', () => { it('should call toggle() when c
我的测试结构如下 describe('Test Suite'){ describe('First Test Case'){ it('1st step'){};
我尝试进行符合最佳实践的 RSpec 测试,并且我知道在测试实例方法时,可以这样做 describe "#my_instance_method" do ... end 但是类方法呢?我应该在描述字符串
我在 Mocha 中有这个简单的示例单元测试: const {Builder, By, Key, until} = require('selenium-webdriver') const chai =
我正在使用 Git Plugin Jenkins ans 使用Douglas Creager's get_git_version脚本。这使用 git describe 为 python 模块获取一些合
我在 Ubuntu 10.10 amd64 上使用 git 1.7.1,我正在尝试提取我的存储库 HEAD 的哈希值,以便在我编译到我的项目中的自动化版本信息中使用它。 过去,这总是通过使用 git
用这个 artist-detail.spec.ts 运行我的测试后 import { HttpClientModule } from '@angular/common/http'; import {
我有一个具有以下输出的 repo: $ git tag product-picker-v1 v0.0.1 v0.0.2 v0.0.3 v0.0.4 v0.1 v0.1.1 v0.1.10 v0.1.1
我有一个具有以下输出的 repo: $ git tag product-picker-v1 v0.0.1 v0.0.2 v0.0.3 v0.0.4 v0.1 v0.1.1 v0.1.10 v0.1.1
在每个应用程序的 Google Play 网页上都有一个权限部分。几乎所有这些应用程序都使用一些标准术语来描述它们的权限,例如“READ PHONE STATUS AND IDENTITY”、“SEN
我想使用 describe() 函数创建一个数据框。正在考虑的数据集是 iris。数据框应如下所示: Variable n missing unique Info Mean
我正在尝试运行角度应用程序。我已经删除 node_module,重新安装它,安装最新的 typescript 版本但我仍然有这个错误:找不到名称'describe',找不到名称'beforeEach'
git describe 有语法吗?只显示最新的标签和额外的提交? 让你得到 4.0.7 用于标记为 4.0.7 的提交4.0.7-12 自标记 4.0.7 以来有 12 次提交 git descri
我们偶尔会在同一个提交上有两个标签。当我们对那个提交使用 git describe 时,git describe 总是返回第一个标签。我对 git-describe 手册页的阅读似乎表明应该返回第二个
我有一个包含原始分数和样本权重的数据文件。现在我想使用 psych 包的 describe 函数,同时考虑样本权重。 有谁知道如何做到这一点,或者是否有某个功能与 psych::describe()
当发出“p4 describe changelist#”时,有时会列出受影响的文件;有时它们不是,只是显示: 受影响的文件... 有谁知道这是为什么? 最佳答案 最可能的解释:您没有受影响文件的“列表
我正在尝试在 oracle 上运行“describe table_name”命令。我正在使用 dbeaver 访问 oracle。但是,当我运行该命令时,它显示 SQLException 和错误消息“
似乎我不理解 SPARQL DESCRIBE 查询。我需要检索与条件匹配的资源的完整图表。在我尝试过的一个 SPARQL 端点(Norwegian Rådata Nå, http://data.bib
在我们的BitBucket中,我们有一些像这样的标签: build-0.0.4.152 0.0.4 build-0.0.3.151 build-0.0.3.150 0.0.3 所有带前缀“bu
据我了解,测试套件中的 it 调用的执行是按顺序发生的,即一个调用只有在前一个调用完成后才会执行。但是,我看到了令人不快的行为,即在第一个请求的回调完成之前执行第二个请求的回调 我不知道我的前提是否错
我是一名优秀的程序员,十分优秀!