liveBook · Manning

Test-driven development

As explained in Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

Test-driven development is a software-development practice in which automated tests are written before the implementation they specify.

Test-driven development (TDD) writes automated tests before implementation. The tests specify how a tool or feature should behave, then code is written until the tests pass. section 8.5 section 20.3

A practical TDD cycle looks like this:

  • Describe the intended behavior through usage examples or tests. section 8.5 section 20.3
  • Run the tests and confirm that the new feature initially fails. section 20.3
  • Implement the feature incrementally until every test passes. section 20.3
  • Keep tests for unusual failures so later changes cannot silently forget those conditions. section 20.3

Specify a machine-information tool

Usage examples such as Get-MachineInfo -ComputerName CLIENT, pipeline input, and optional failure logging expose the desired interface before implementation. If the examples become unwieldy, simplify the tool design or split responsibilities. section 8.5

Get-MachineInfo -ComputerName CLIENT
Get-MachineInfo -ComputerName CLIENTA,CLIENTB
Get-MachineInfo -ComputerName (Get-Content names.txt)
Get-MachineInfo -ComputerName (Get-ADComputer -id CLIENTA | 
Select -Expand name)
Get-Content names.txt | Get-MachineInfo
Get-ADComputer -id CLIENTA | Select -Expand name | Get-MachineInfo

Compare direct arguments, lists from files, directory-service results, and pipeline input as concrete behavioral specifications for one command. section 8.5

How Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools covers it

How deep it goes
Explains the test-first cycle and regression value, but stops short of naming a testing framework or showing test code.
Examples
Uses Get-MachineInfo usage forms and optional failure logging as concrete specifications for command behavior.
What it assumes
Assumes enough command and tool-design knowledge to write usage examples, while deriving behavior from those examples rather than implementation details.
What you can do after
Apply it via automated tests that define a feature, expose its initial failure, and guide implementation until behavior passes.
How it connects
Connects usage examples, functional specification, automated testing, tool design, and regression protection.

Also explained in

Compare how 15 books explain test-driven development →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools. The section links point to the parts of the book it draws on.