-
-
Notifications
You must be signed in to change notification settings - Fork 525
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add testing documentation #1416
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
Title: Testing Spectre CLI Apps | ||
Description: "Test Spectre CLI features,functionality, output, and performance." | ||
--- | ||
|
||
The `CommandAppTester` class is used to test a Spectre CLI application. | ||
It should be used to test a `CommandApp` `Command`s and `CommandSetting`s. | ||
|
||
## CommandAppTester | ||
|
||
The `CommandAppTester` class is a wrapper around the `CommandApp` object configured for testing. | ||
|
||
### Example | ||
|
||
```csharp | ||
public class Program | ||
{ | ||
public static int Main(string[] args) | ||
{ | ||
var app = new CommandApp(); | ||
app.Configure(appConfig); | ||
var result = app.Run(args); | ||
return result; | ||
} | ||
|
||
public static void appConfig(IConfigurator config) | ||
{ | ||
config.AddCommand<HelloCommand>("hello"); | ||
} | ||
} | ||
|
||
public class HelloCommand : Command | ||
{ | ||
public override int Execute(CommandContext context) | ||
{ | ||
AnsiConsole.WriteLine("Hello world"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Command should not use AnsiConsole directly, since this doesn't play well with testing. The command should inject a |
||
return 0; | ||
} | ||
} | ||
|
||
public class ProgramTests | ||
{ | ||
[Fact] | ||
public void Should_Return_Hello_World() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great example! 👍 |
||
{ | ||
// Given | ||
var app = new CommandAppTester(); | ||
app.Configure(Program.appConfig); | ||
|
||
// When | ||
var result = app.Run("hello"); | ||
|
||
// Then | ||
Assert.Multiple(() => | ||
{ | ||
Assert.Equal(0, result.ExitCode); | ||
Assert.Equal("Hello world", result.Output); | ||
}); | ||
} | ||
} | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
Title: Testing Spectre Console | ||
Description: "Test Spectre Console features,functionality, output, and performance." | ||
--- | ||
|
||
The `TestConsole` class is used to test the output of `AnsiConsole`. | ||
|
||
## TestConsole | ||
|
||
The `TestConsole` class is a wrapper around the `AnsiConsole` object configured for testing, with no (current) support for input. | ||
|
||
## Example | ||
|
||
```csharp | ||
public class ProgramTests | ||
{ | ||
[Fact] | ||
public void Should_Return_Hello_World() | ||
{ | ||
// Given | ||
var console = new TestConsole(); | ||
// When | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Add line break |
||
console.WriteLine("Hello world"); | ||
// Then | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Add line break |
||
Assert.Equal("Hello world" + Environment.NewLine, console.Output); | ||
} | ||
} | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
Title: Spectre.Console.Testing | ||
Order: 90 | ||
Description: "The testing suite for the command line interface and spectre console...." | ||
--- | ||
|
||
<p>Spectre.Console.Testing is the namespace for testing suite of Spectre Console and CLI. | ||
The <a href="https://github.com/VerifyTests/Verify">Verify</a> package can also be used to verify console output. | ||
</p> | ||
|
||
<ul> | ||
@foreach(IDocument child in OutputPages.GetChildrenOf(Document)) | ||
{ | ||
<li>@Html.DocumentLink(child)</li> | ||
} | ||
</ul> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we rename this to something like
ConfigureCommandApp
?