mirror of
https://github.com/spectreconsole/spectre.console.git
synced 2025-12-26 07:47:56 +01:00
Update docs related to CLI unit testing
This commit is contained in:
@@ -1,17 +1,17 @@
|
||||
Title: Unit Testing
|
||||
Order: 14
|
||||
Description: Instructions for unit testing a Spectre.Console application.
|
||||
Description: Instructions for unit testing a Spectre.Console.Cli application.
|
||||
---
|
||||
|
||||
`Spectre.Console` has a separate project that contains test harnesses for unit testing your own console applications.
|
||||
`Spectre.Console.Cli` has a separate project that contains test harnesses for unit testing your own console applications.
|
||||
|
||||
The fastest way of getting started is to install the `Spectre.Console.Testing` NuGet package.
|
||||
The fastest way of getting started is to install the `Spectre.Console.Cli.Testing` NuGet package.
|
||||
|
||||
```text
|
||||
> dotnet add package Spectre.Console.Testing
|
||||
> dotnet add package Spectre.Console.Cli.Testing --prerelease
|
||||
```
|
||||
|
||||
`Spectre.Console.Testing` is also the namespace containing the test classes.
|
||||
`Spectre.Console.Cli.Testing` is also the namespace containing the test classes.
|
||||
|
||||
## Testing a CommandApp
|
||||
|
||||
@@ -141,55 +141,4 @@ public sealed class InteractiveCommandTests
|
||||
result.Output.EndsWith("[Apple;Apricot;Spectre Console]");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Testing console behaviour
|
||||
|
||||
`TestConsole` and `TestConsoleInput` are testable implementations of `IAnsiConsole` and `IAnsiConsoleInput`, allowing you fine-grain control over testing console output and interactivity.
|
||||
|
||||
The following example renders some widgets before then validating the console output:
|
||||
|
||||
```csharp
|
||||
[TestMethod]
|
||||
public void Should_Render_Panel()
|
||||
{
|
||||
// Given
|
||||
var console = new TestConsole();
|
||||
|
||||
// When
|
||||
console.Write(new Panel(new Text("Hello World")));
|
||||
|
||||
// Then
|
||||
Assert.AreEqual(console.Output, """"
|
||||
┌─────────────┐
|
||||
│ Hello World │
|
||||
└─────────────┘
|
||||
|
||||
"""");
|
||||
}
|
||||
```
|
||||
|
||||
While `Assert` is fine for validating simple output, more complex output may benefit from a tool like [Verify](https://github.com/VerifyTests/Verify).
|
||||
|
||||
The following example prompts the user for input before then validating the expected choice was made:
|
||||
|
||||
```csharp
|
||||
[TestMethod]
|
||||
public void Should_Select_Orange()
|
||||
{
|
||||
// Given
|
||||
var console = new TestConsole();
|
||||
console.Input.PushTextWithEnter("Orange");
|
||||
|
||||
// When
|
||||
console.Prompt(
|
||||
new TextPrompt<string>("Favorite fruit?")
|
||||
.AddChoice("Banana")
|
||||
.AddChoice("Orange"));
|
||||
|
||||
// Then
|
||||
Assert.AreEqual(console.Output, "Favorite fruit? [Banana/Orange]: Orange\n");
|
||||
}
|
||||
```
|
||||
|
||||
`CommandAppTester` uses `TestConsole` internally, which in turn uses `TestConsoleInput`, offering a fully testable harness for `Spectre.Console` widgets, prompts and commands.
|
||||
```
|
||||
63
docs/input/unit-testing.md
Normal file
63
docs/input/unit-testing.md
Normal file
@@ -0,0 +1,63 @@
|
||||
Title: Unit Testing
|
||||
Order: 24
|
||||
Description: Instructions for unit testing a Spectre.Console application.
|
||||
---
|
||||
|
||||
`Spectre.Console` has a separate project that contains test harnesses for unit testing your own console applications.
|
||||
|
||||
The fastest way of getting started is to install the `Spectre.Console.Testing` NuGet package.
|
||||
|
||||
```text
|
||||
> dotnet add package Spectre.Console.Testing
|
||||
```
|
||||
|
||||
`Spectre.Console.Testing` is also the namespace containing the test classes.
|
||||
|
||||
## Testing console behaviour
|
||||
|
||||
`TestConsole` and `TestConsoleInput` are testable implementations of `IAnsiConsole` and `IAnsiConsoleInput`, allowing you fine-grain control over testing console output and interactivity.
|
||||
|
||||
The following example renders some widgets before then validating the console output:
|
||||
|
||||
```csharp
|
||||
[TestMethod]
|
||||
public void Should_Render_Panel()
|
||||
{
|
||||
// Given
|
||||
var console = new TestConsole();
|
||||
|
||||
// When
|
||||
console.Write(new Panel(new Text("Hello World")));
|
||||
|
||||
// Then
|
||||
Assert.AreEqual(console.Output, """"
|
||||
┌─────────────┐
|
||||
│ Hello World │
|
||||
└─────────────┘
|
||||
|
||||
"""");
|
||||
}
|
||||
```
|
||||
|
||||
While `Assert` is fine for validating simple output, more complex output may benefit from a tool like [Verify](https://github.com/VerifyTests/Verify).
|
||||
|
||||
The following example prompts the user for input before then validating the expected choice was made:
|
||||
|
||||
```csharp
|
||||
[TestMethod]
|
||||
public void Should_Select_Orange()
|
||||
{
|
||||
// Given
|
||||
var console = new TestConsole();
|
||||
console.Input.PushTextWithEnter("Orange");
|
||||
|
||||
// When
|
||||
console.Prompt(
|
||||
new TextPrompt<string>("Favorite fruit?")
|
||||
.AddChoice("Banana")
|
||||
.AddChoice("Orange"));
|
||||
|
||||
// Then
|
||||
Assert.AreEqual(console.Output, "Favorite fruit? [Banana/Orange]: Orange\n");
|
||||
}
|
||||
```
|
||||
Reference in New Issue
Block a user