You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/docfx.json
+7-2Lines changed: 7 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,11 @@
3
3
{
4
4
"src": [
5
5
{
6
-
"files": [ "bunit.csproj" ],
6
+
"files": [
7
+
"bunit.core/bunit.core.csproj",
8
+
"bunit.web/bunit.web.csproj",
9
+
"bunit.xunit/bunit.xunit.csproj"
10
+
],
7
11
"exclude": [ "**/bin/**", "**/obj/**" ],
8
12
"src": "../src"
9
13
}
@@ -65,7 +69,8 @@
65
69
"_appTitle": "bUnit",
66
70
"_description": "bUnit is a unit testing library for Blazor Components. You can easily define components under test in C# or Razor syntax and verify outcome using semantic HTML diffing/comparison logic. You can interact with and inspect components, trigger event handlers, provide cascading values, inject services, mock IJsRuntime, and perform snapshot testing.",
Copy file name to clipboardExpand all lines: docs/docs/csharp-test-examples.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,7 @@
1
1
# C# test examples
2
2
3
+
> **WARNING:** These examples are somewhat outdated. Some content in here might still apply:
4
+
3
5
In the following examples, the terminology **component under test** (abbreviated CUT) is used to mean the component that is the target of the test. The examples below use the `Shouldly` assertion library as well. If you prefer not to use that just replace the assertions with the ones from your own favorite assertion library.
4
6
5
7
All examples can be found in the [Tests](https://github.com/egil/razor-components-testing-library/tree/master/sample/tests/Tests) folder in the [Sample project](https://github.com/egil/razor-components-testing-library/tree/master/sample/).
Copy file name to clipboardExpand all lines: docs/docs/index.md
+11-9Lines changed: 11 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,28 +8,30 @@ The sections below takes you through each of the steps you need to get started:
8
8
9
9
First you need a test project to store your component tests inside. Depending on what general purpose unit testing framework you prefer, you have a few options:
10
10
11
-
-[Creating a new bUnit and xUnit test project](creating-a-new-bunit-xunit-project.html)
12
-
-[Creating a new bUnit and NUnit test project](creating-a-new-bunit-nunit-project.html)
13
-
-[Creating a new bUnit and MSTest test project](creating-a-new-bunit-mstest-project.html)
11
+
-[Creating a new bUnit and xUnit test project](/docs/creating-a-new-bunit-xunit-project.html)
12
+
-[Creating a new bUnit and NUnit test project](/docs/creating-a-new-bunit-nunit-project.html)
13
+
-[Creating a new bUnit and MSTest test project](/docs/creating-a-new-bunit-mstest-project.html)
14
14
15
15
## The basics of testing Blazor components
16
16
17
17
To test a component, you first have to render it with parameters, cascading values, and services passed into it. Then, you need access to the component's instance and the markup it has produced, so you can inspect and interact with both.
18
18
19
19
There are three different ways of doing this in bUnit:
20
20
21
-
1.[**C# based tests**](csharp-based-testing.html)
21
+
1.[**C# based tests**](/docs/csharp-based-testing.html)
22
22
With C# based tests, you write all your testing logic in C# files, i.e. like regular unit tests.
23
-
2.[**Razor based tests**](razor-based-testing.html)
23
+
2.[**Razor based tests**](/docs/razor-based-testing.html)
24
24
With Razor based tests, you write tests in `.razor` files, which allows you to declare, in Razor syntax, the component under test and other markup fragments you need. You still write your assertions via C# in the .razor file, inside `@code {...}` blocks.
Snapshot tests are written in `.razor` files. A test contains a definition of an input markup/component and the expected output markup. The library will then automatically perform an semantic HTML comparison. Very little C# is needed in this, usually only to configure services.
27
27
28
-
To learn more, read the [Basics of Blazor component testing](basics-of-blazor-component-testing.html) page.
28
+
To learn more, read the [Basics of Blazor component testing](/docs/basics-of-blazor-component-testing.html) page.
29
29
30
30
## Examples
31
31
32
32
To see examples of how to work assert and manipulate a rendered component, go to the following pages:
33
33
34
-
-[C# test examples](/docs/csharp-test-examples.html)
35
-
-[Razor test examples](/docs/razor-test-examples.html)
34
+
-[C# test examples in the sample project](https://github.com/egil/bunit/tree/master/sample/tests/Tests)
35
+
-[Razor based test examples in the sample project](https://github.com/egil/bunit/tree/master/sample/tests/RazorTestComponents)
36
+
-[Snapshot test examples in the sample project](https://github.com/egil/bunit/tree/master/sample/tests/SnapshotTests)
This pages documents how to do Blazor/Razor component testing from `.razor` files.
4
-
5
-
Before you get started, make sure you have read the [Getting started](/docs/getting-started.html) page and in particular the [Basics of Blazor component testing](/docs/basics-of-blazor-component-testing.html) section. It wont take long, and it will ensure you get a good start at component testing.
6
-
7
-
> **NOTE:** This feature is _EXPERIMENTAL_ and syntax and API will likely changed. Here are a few limitations to be aware of at the moment:
8
-
>
9
-
> - The xUnit test runner can detect and execute tests in Razor test components, but is not able to distinguish the individual `<Fixture>`'s from each other. They are all executed together, one at the time. The solution is planned, see the [related issue](https://github.com/egil/razor-components-testing-library/issues/4) for details.
10
-
> - Go to the [Contribute](/docs/contribute) page for info on how to provide feedback and suggestions.
11
-
12
-
> **TIP:** Working with and asserting against the rendered component and its output is covered on the [Working with rendered components and fragments](/docs/working-with-rendered-components-and-fragments.html) page.
13
-
14
-
**Content:**
15
-
16
-
-[Creating a new Razor test component](#creating-a-new-razor-test-component)
17
-
-[Defining tests/fixtures in test components](#defining-testsfixtures-in-test-components)
18
-
-[Executing test cases](#executing-test-cases)
3
+
> **WARNINIG**: This feature is only supported when using bUnit with xUNit. NUnit and MSTest is planned.
19
4
20
-
**Further reading:**
21
-
22
-
-[Working with rendered components and fragments](/docs/working-with-rendered-components-and-fragments.html)
23
-
-[Semantic HTML markup comparison](/docs/semantic-html-markup-comparison.html)
-[Razor test examples](/docs/razor-test-examples.html)
5
+
This pages documents how to do Blazor/Razor component testing from `.razor` files.
26
6
27
7
## Creating a new Razor test component
28
8
29
-
To create Razor based tests, we need to create test components. All test components must inherit from `TestComponentBase`, e.g. by adding `@inherits TestComponentBase` to the top of your .razor file. The `TestComponentBase` contains all the logic for rendering components and correctly dispose of renderers, components, and HTML parsers after each test.
9
+
To create Razor based tests, we need to create Blazor test components. All test components must inherit from `TestComponentBase`, e.g. by adding `@inherits TestComponentBase` to the top of your .razor file.
30
10
31
11
For example:
32
12
@@ -49,14 +29,12 @@ For example:
49
29
You will also need to import a few namespaces to make asserting and mocking possible. They are best placed in an `_Imports.razor` file next to your Razor test components, e.g.:
50
30
51
31
```cshtml
52
-
@using Microsoft.AspNetCore.Components.Web
53
-
@using Microsoft.Extensions.DependencyInjection
54
32
@using Bunit
55
33
@using Bunit.Mocking.JSInterop
56
34
@using Xunit
57
35
```
58
36
59
-
> **NOTE:** The `_Imports.razor` has already been created for you if you are using the [Blazor test project template](/docs/creating-a-new-test-project.html).
37
+
> **NOTE:** The `_Imports.razor` has already been created for you if you are using the [Blazor test project template](/docs/creating-a-new-bunit-xunit-project.html).
60
38
61
39
## Defining tests/fixtures in test components
62
40
@@ -65,13 +43,12 @@ When you have a Razor test component created, its time to add test cases/fixture
65
43
Lets look at what options we have by setting up an empty test case, first the code:
66
44
67
45
```cshtml
68
-
<Fixture Description="MyComponent renders as expected" @* Optional - description is shown in error message if test fails *@
69
-
Setup="Setup" @* Optional - method called first *@
70
-
SetupAsync="SetupAsync" @* Optional - method called after Setup *@
71
-
Test="Test1" @* Optional - method called after Setup/SetupAsync *@
72
-
TestAsync="Test1Async" @* Optional - method called after Test *@
73
-
Tests="new Action[]{ Test2, Test3 }"> @* Optional - methods are called after Test/TestAsync, one at the time *@
74
-
TestsAsync="new Func<Task>[]{ Test2Async, Test3Async }"> @* Optional - methods are called after Tests, one at the time *@
46
+
<Fixture Description="MyComponent renders as expected"
47
+
Timeout="1000"
48
+
Skip="Reason to skip the test"
49
+
Setup="Setup
50
+
SetupAsync="SetupAsync"
51
+
Test="Test1" @* or *@ TestAsync="Test1Async">
75
52
<ComponentUnderTest>
76
53
<MyComponent />
77
54
</ComponentUnderTest>
@@ -85,56 +62,38 @@ Lets look at what options we have by setting up an empty test case, first the co
85
62
@code {
86
63
// Called first if present when added to the Setup parameter
87
64
// on a <Fixture> component (can be named anything)
88
-
void Setup()
65
+
void Setup(Fixture fixture)
89
66
{
90
67
// Add services and do other setup work in this method.
91
-
Services.AddMockJsRuntime();
68
+
fixture.Services.AddMockJsRuntime();
92
69
}
93
70
94
71
// Called after Setup if present when added to the Setup parameter
95
72
// on a <Fixture> component (can be named anything)
@@ -144,13 +103,12 @@ The code above works as follows:
144
103
145
104
1.`Setup`
146
105
2.`SetupAsync`
147
-
3.`Test`
148
-
4.`TestAsync`
149
-
5.`Tests`, one at the time, in the order they appear in the array.
150
-
6.`TestsAsync`, one at the time, in the order they appear in the array.
106
+
3.`Test` or `TestAsync`
151
107
152
-
-The `Description` parameter on the `<Fixture>` element is displayed in the test runners error window if the test fails.
108
+
-If the `Description` parameter is present, it is used as the name of the test in the Test Explorer and is displayed in the test runners error window if the test fails.
153
109
- It is inside child component `<ComponentUnderTest>` where you declare the component under test.
110
+
- If the `Timeout` parameter is specified, the test will timeout after the specified time, in milliseconds.
111
+
- If `Skip` parameter is specified, the test is skipped and the reason is printed in the test output.
154
112
- Any markup or component fragments that is needed for the test can be declared inside the optional `<Fragment>` components. The `Id` parameter is optional, and only needed if you have more than one.
155
113
156
114
- To render and get the component under test or any of the fragments, use the `GetComponentUnderTest<TComponent>()` method, where `TComponent` is the type of the component you have defined under the `<ComponentUnderTest>` element.
@@ -163,4 +121,8 @@ The code above works as follows:
163
121
164
122
Since Blazor test component use xUnit under the hood as a test runner, you execute your tests them in exactly the same way as you would normal xUnit unit tests, i.e. by running `dotnet test` from the console or running the tests through the Test Explorer in Visual Studio.
165
123
166
-
Do note the current limitations mentioned at the top of the page.
124
+
## Further reading:
125
+
126
+
-[Semantic HTML markup comparison](/docs/semantic-html-markup-comparison.html)
Copy file name to clipboardExpand all lines: docs/docs/razor-test-examples.md
+7-5Lines changed: 7 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,14 @@
1
1
# Razor test examples
2
2
3
+
> **WARNING:** These examples are somewhat outdated. Some content in here might still apply:
4
+
3
5
In the following examples, the terminology **component under test** (abbreviated CUT) is used to mean the component that is the target of the test. The examples below use the `Shouldly` assertion library as well. If you prefer not to use that just replace the assertions with the ones from your own favorite assertion library.
4
6
5
-
All examples can be found in the [Tests](https://github.com/egil/razor-components-testing-library/tree/master/sample/tests/Tests) folder in the [Sample project](https://github.com/egil/razor-components-testing-library/tree/master/sample/).
7
+
All examples can be found in the [Tests](https://github.com/egil/bunit/tree/master/sample/tests/Tests) folder in the [Sample project](https://github.com/egil/bunit/tree/master/sample/).
6
8
7
9
## Examples
8
10
9
-
Here is a few examples that demonstrate how Razor test components can be used. More can be found in the [sample/tests/RazorComponentTests](https://github.com/egil/razor-components-testing-library/tree/master/sample/tests/RazorComponentTests) samples folder.
11
+
Here is a few examples that demonstrate how Razor test components can be used. More can be found in the [sample/tests/RazorComponentTests](https://github.com/egil/bunit/tree/master/sample/tests/RazorComponentTests) samples folder.
@@ -30,7 +32,7 @@ Here is a few examples that demonstrate how Razor test components can be used. M
30
32
}
31
33
```
32
34
33
-
This example shows how [ThemedElement.razor](https://github.com/egil/razor-components-testing-library/tree/master/sample/src/Components/ThemedElement.razor) can be tested with cascading values.
35
+
This example shows how [ThemedElement.razor](https://github.com/egil/bunit/tree/master/sample/src/Components/ThemedElement.razor) can be tested with cascading values.
34
36
35
37
```cshtml
36
38
<Fixture Test=MarkupPassedViaChildContent>
@@ -53,9 +55,9 @@ This example shows how [ThemedElement.razor](https://github.com/egil/razor-compo
53
55
}
54
56
```
55
57
56
-
This example shows how [ThemedButton.razor](https://github.com/egil/razor-components-testing-library/tree/master/sample/src/Components/ThemedButton.razor) can be tested with with child content, and how a `<Fragment>` can be used to specify the expected output.
58
+
This example shows how [ThemedButton.razor](https://github.com/egil/bunit/tree/master/sample/src/Components/ThemedButton.razor) can be tested with with child content, and how a `<Fragment>` can be used to specify the expected output.
57
59
58
-
Lets look at a more complex example, a test of the [TodoList.razor](https://github.com/egil/razor-components-testing-library/tree/master/sample/src/Pages/TodoList.razor) component:
60
+
Lets look at a more complex example, a test of the [TodoList.razor](https://github.com/egil/bunit/tree/master/sample/src/Pages/TodoList.razor) component:
Copy file name to clipboardExpand all lines: docs/docs/semantic-html-markup-comparison.md
-7Lines changed: 0 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,13 +6,6 @@ On this page we will go through how the comparison works, and what options you h
6
6
7
7
> **NOTE:** The semantic HTML comparison is available in all three test types, but is always used in the Snapshot test type.
8
8
9
-
**Content:**
10
-
11
-
-[Why semantic comparison is needed for stable tests](#why-semantic-comparison-is-needed-for-stable-tests)
12
-
-[Customizing the comparison process](#customizing-the-comparison-process)
13
-
-[Verifying output from components](#verifying-output-from-components)
14
-
-[Different ways of getting the differences](#different-ways-of-getting-the-differences)
15
-
16
9
## Why semantic comparison is needed for stable tests
17
10
18
11
Just performing string comparison of two strings containing HTML markup can break quite easily, _even_ if the two markup strings are semantically equivalent. Some changes that can cause a regular string comparison to fail are:
0 commit comments