Comments (5)
In case it helps anybody, you can add this little incantation to BeforeAll to import functions from a script without executing it.
function Get-Functions($filePath)
{
$script = Get-Command $filePath
return $script.ScriptBlock.AST.FindAll({ $args[0] -is [Management.Automation.Language.FunctionDefinitionAst] }, $false)
}
Get-Functions ScriptPath.ps1 | Invoke-Expression
from pester.
Hi Andy, I'm considering closing this issue. It looks like you found a workaround in your SO question and I don't necessarily think this is something that Pester itself needs to solve. You can always using the Get-Functions command in your Tests.ps1 file to get the functions.
Perhaps this requires more discussion on testing strategies in PowerShell. In the example you provided I would simply move those functions into a separate file. Then you can simply dot source and not need the Get-Functions function.
End-to-end acceptance tests is something I'm trying to figure out how to do with Pester.
from pester.
wrt moving functions into their own file - I think the testing framework should support the various ways that PowerShell allows you to organize your code. I see it as a limitation to force a certain style of code organization.
Having function code and non-function code in the same PowerShell script is perfectly valid. It also makes dissemination easier as it only requires a single file.
It would be nice to have this built into the framework so that it has support for this scenario out of the box.
I'm thinking perhaps instead of -
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".")
. "$here$sut"
There could be a built in function (like "Describe") for importing (Imports maybe) that does the job of the above and also only brings in the functions with the Get-Functions command.
This only works however when the functions are autonomous (work without variables created in higher scopes). Though I think this isn't too much to ask because this is a best practice for functions anyway.
from pester.
Thanks for the well articulated points.
I'm tempted to spin this off as a separate project. This project would include functions like:
Require
and
Require-Relative
This way it can be used by other projects besides Pester. How does this sound? I really like the point about Pester not limit the style on how you organize your code. So as long as you can load your functions from a Pester Tests file (using any loading lib), than it should be fine.
I like how python makes this easy by having a conventional function for script execution vs import. How would this be handled by ruby, perl, or any other scripting language?
from pester.
Closing. I think this would make for an excellent project. eg: ImportPS or something. Create a module, and I'll happily add a dependency to Pester.
from pester.
Related Issues (20)
- v5.5 $____Pester.CurrentTest not populated in AfterEach HOT 4
- Missing timestamp from test suite attributes in junit xml export HOT 1
- Unbound container scriptblock unexpectedly runs in Pester scope HOT 10
- Pester 5.5 reports incorrect Skipped results for Pending and Inconclusive HOT 3
- BeOfType doesn't see types, loaded from a file with classes, but the "It" function - does HOT 3
- Refer to a script in a parent directory HOT 2
- Does somthing like @afterstep exist for Pester 4.6 with Invoke Gherkin HOT 1
- New-RandomTempRegistry: Test-Path : No more data is available HOT 2
- Provide a way to more easily modify a mocked parameter before comparing with a test value. HOT 3
- Error during handling of output-element within Write-NUnit3OutputElement HOT 1
- SkipRemainingOnFailure does not function correctly when there are nested BeforeAll blocks HOT 2
- Support shuffling / randomized test runs HOT 7
- Templates in test names do not expand variables when skipped HOT 8
- Add test count to minimal view HOT 4
- Remove -Pending from Set-ItResult HOT 1
- Remove legacy Should Syntax
- Certificate warning when upgrading to 5.6.0-beta1 HOT 1
- Block setup and teardown executed when tests are skipped HOT 4
- SkipRemainingOnFailure reports skipped on filtered and explicitly skipped tests
- $configuration.CodeCoverage.UseBreakpoints = to false by default
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pester.