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/docs/external-resources.md
+5Lines changed: 5 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,3 +1,8 @@
1
+
---
2
+
uid: external-resources
3
+
title: External Resources
4
+
---
5
+
1
6
# Presentations, guides, articles, tutorials, and blog posts
2
7
Here is a list of some of the content out on the web that covers bUnit. If you produce something you think can be useful to bUnit's users, feel free to add the content to this page and send a pull request.
Copy file name to clipboardExpand all lines: docs/docs/getting-started/create-test-project.md
+5-5Lines changed: 5 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,15 +1,15 @@
1
1
---
2
2
uid: create-test-project
3
-
title: Creating a new bUnit test project
3
+
title: Creating a new bUnit Test Project
4
4
---
5
5
6
-
# Creating a new bUnit test project
6
+
# Creating a new bUnit Test Project
7
7
8
8
Before you can write any tests, you need a place to put them - a test project. bUnit is not a unit test runner, so you need a general purpose test framework, like xUnit, NUnit, or MSTest, in addition to bUnit, to run your tests, and write your assertions.
9
9
10
10
If you prefer xUnit, you can use the bUnit project template approached described in the [Create a test project with bUnit template](#create-a-test-project-with-bunit-template) section further down the page. If you want to use another general purpose testing framework, read the following section.
11
11
12
-
## Create a test project manually
12
+
## Create a Test Project Manually
13
13
14
14
To create a project for testing you Blazor components that uses either of three general purpose test frameworks, you need to go through these steps:
15
15
@@ -183,7 +183,7 @@ The end result should be a test project with a `.csproj` that looks like this (o
183
183
184
184
***
185
185
186
-
## Create a test project with bUnit template
186
+
## Create a Test Project with bUnit Template
187
187
188
188
If you want to skip a few steps in the guide above, you can use the [bUnit test project template](https://www.nuget.org/packages/bunit.template/). The bUNit project template is only available for using with xUnit as the general purpose testing framework, but that will change in the future.
189
189
@@ -223,7 +223,7 @@ dotnet sln <NAME OF PROJECT>.sln add <NAME OF TEST PROJECT>
223
223
dotnet add <NAME OF COMPONENT PROJECT>.csproj reference <NAME OF TEST PROJECT>.csproj
224
224
```
225
225
226
-
## Further reading
226
+
## Further Reading
227
227
228
228
Now you are ready to write some times. To learn how, continue reading the <xref:writing-csharp-tests> and <xref:writing-razor-tests> pages.
bUnit's <xref:Bunit.Fixture> component provides different parameters you can set on it, that changes the behavior of the test. It also allow you to both set up a component under test, and additional fragments, that can be used in the test.
9
+
10
+
11
+
## Parameters
12
+
13
+
All the parameters the `<Fixture>` support are shown in the listen below:
Let's start by looking at the parameters that takes a method as input first. The methods are called in the order they are listed in below, if provided, and should be used to the following:
20
+
21
+
1.**<xref:Bunit.RazorTesting.FixtureBase`1.Setup>** and **<xref:Bunit.RazorTesting.FixtureBase`1.SetupAsync>:**
22
+
The `Setup` and `SetupAsync` method is called first, and you can provide both if needed. If both are provided, `Setup` is called first.
23
+
They allows you to configures e.g. the <xref:Bunit.ITestContext.Services> collection of the <xref:Bunit.Fixture> component before the component under test or any fragments are rendered.
24
+
2.**<xref:Bunit.RazorTesting.FixtureBase`1.Test>** or **<xref:Bunit.RazorTesting.FixtureBase`1.TestAsync>:**
25
+
The `Test` or `TestAsync` methods are called after the setup methods.
26
+
_One, and only one,_ of the test methods must be specified. Use the test method to access the component under test and any fragments defined in the fixture, and interact and assert against them.
27
+
28
+
**Other parameters**
29
+
30
+
The other parameters affect how the test runs, and how it is displayed in e.g. Visual Studio's Test Explorer:
If a description is provided, it will be displayed by the test runner when the test runs, and in Visual Studio's Test Explorer. If no description is provided, the name of the provided test method is used.
If the skip parameter is provided, the test is skipped and the text entered in the skip parameter is passed to the test runner as the reason to skip the test.
Copy file name to clipboardExpand all lines: docs/docs/getting-started/index.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,9 @@
1
1
---
2
2
uid: getting-started
3
-
title: Getting started with bUnit
3
+
title: Getting Started with bUnit
4
4
---
5
5
6
-
# Getting started with bUnit
6
+
# Getting Started with bUnit
7
7
8
8
To get started writing tests for your Blazor components, you first need to set up a test project. Then you can start writing your tests, using either C# or Razor syntax.
Copy file name to clipboardExpand all lines: docs/docs/getting-started/writing-csharp-tests.md
+5-5Lines changed: 5 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,17 +1,17 @@
1
1
---
2
2
uid: writing-csharp-tests
3
-
title: Writing tests in C# for Blazor components
3
+
title: Writing Tests in C# for Blazor Components
4
4
---
5
5
6
-
# Writing tests in C# for Blazor components
6
+
# Writing Tests in C# for Blazor Components
7
7
8
8
Testing Blazor components is a different from testing regular C# classes: Blazor components are *rendered*, they have the *Blazor component life-cycle*, during which we can *provide input* to them and where they *produce output*.
9
9
10
10
**bUnit** enables you to render the component you want to test, pass in parameters to it, inject services into it, and access the rendered component instance and the markup it has produced.
11
11
12
12
Rendering a component happens through bUnit's <xref:Bunit.TestContext>, and the result of the rendering, a <xref:Bunit.IRenderedComponent`1>, provides access to the component instance, and the markup produced by the component.
13
13
14
-
## Creating a basic test
14
+
## Creating a Basic Test
15
15
16
16
Lets see a simple example, where we test the following `<HelloWorld>` component:
17
17
@@ -50,7 +50,7 @@ In this test, we do the following:
50
50
> [!TIP]
51
51
> In bUnit tests, we like to use the abbreviation `CUT`, short for "component under test", to indicate the component that is being tested. This is inspired by the common testing abbreviation `SUT`, short for "system under test".
52
52
53
-
## Remove boilerplate code from tests
53
+
## Remove Boilerplate Code from Tests
54
54
55
55
We can remove some boilerplate code from each test by making the <xref:Bunit.TestContext> implicitly available to the test class, so we do not have to have `using var ctx = new Bunit.TestContext();` in every test. This can be done like this:
56
56
@@ -82,7 +82,7 @@ Then methods like <xref:Bunit.TestContext.RenderComponent``1(Bunit.Rendering.Com
82
82
83
83
***
84
84
85
-
## Further reading
85
+
## Further Reading
86
86
87
87
With the basics out of the way, we will next look at how to pass parameters and inject services into our components under test, and after that, cover in more details the various ways we can verify the outcome of a rendering.
Copy file name to clipboardExpand all lines: docs/docs/getting-started/writing-razor-tests.md
+9-9Lines changed: 9 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,29 +1,29 @@
1
1
---
2
2
uid: writing-razor-tests
3
-
title: Writing tests in Razor syntax for Blazor components
3
+
title: Writing Tests in Razor Syntax for Blazor Components
4
4
---
5
5
6
-
# Writing tests in Razor syntax for Blazor components
6
+
# Writing Tests in Razor Syntax for Blazor Components
7
7
8
8
> [!WARNING]
9
9
> Razor tests are currently only compatible with using xUnit as the general purpose testing framework.
10
10
11
11
A test for a Blazor component can be written in a Blazor component, using a mix of Razor and C# syntax. The advantage of this is the familiarity in declaring the component under test, and other HTML or Razor fragments that will be used in the test, in Razor and HTML markup. This is especially useful when testing components that takes a lot of parameters and child content as input.
12
12
13
-
> [!TIP]
13
+
> [!INFO]
14
14
> Tests declared inside Blazor test components can be discovered and invoked individually, and will show up in e.g. Visual Studio's Test Explorer.
15
15
>
16
16
> However, they will _not_ show up before the Blazor test component has been compiled into C# by the Blazor compiler, and if there are compile-errors from the Blazor compiler, they might appear to come and go in the Test Explorer.
17
17
18
-
## Create a test specific`_Imports.razor`file
18
+
## Create a Test Specific`_Imports.razor`File
19
19
20
20
Razor tests are written in Blazor test components. To make our life's a little easier, let's first set up a `_Imports.razor` file, with the using statements we are going to be using throughout our tests. Simply add the following `_Imports.razor` to the root folder where you will be placing your Blazor test components:
With that created, we are ready to create our first Razor test.
25
25
26
-
## Creating a Blazor test component
26
+
## Creating a Blazor Test Component
27
27
28
28
A Blazor test component is conceptually very similar to a regular test class in e.g. xUnit or NUnit. You can define multiple tests inside a single test component, but those has to based on the special bUnit test components, currently either <xref:Bunit.Fixture> or <xref:Bunit.SnapshotTest>.
29
29
@@ -33,7 +33,7 @@ Besides that, Blazor test components has to inherit from <xref:Bunit.TestCompon
33
33
34
34
The following two sections will show how to create tests using bUnit's <xref:Bunit.Fixture> and <xref:Bunit.SnapshotTest> components.
35
35
36
-
### Creating a test using the `<Fixture>`component
36
+
### Creating a Test using the `<Fixture>`Component
37
37
38
38
Lets see a simple example, where we test the following `<HelloWorld>` component using the bUnit <xref:Bunit.Fixture> component:
39
39
@@ -59,7 +59,7 @@ Let's break down what is going on in this test:
59
59
> [!TIP]
60
60
> In bUnit tests, we like to use the abbreviation `CUT`, short for "component under test", to indicate the component that is being tested. This is inspired by the common testing abbreviation `SUT`, short for "system under test".
61
61
62
-
### Creating a test using the `<SnapshotTest>`component
62
+
### Creating a Test using the `<SnapshotTest>`Component
63
63
64
64
In snapshot testing, you declare your input (e.g. one or more component under test) and the expected output, and the library will automatically tell you if they do not match. With bUnit, this comparison is done using a smart built-in semantic HTML comparison logic.
65
65
@@ -82,7 +82,7 @@ When the test runs, the <xref:Bunit.SnapshotTest> component will automatically c
82
82
> [!TIP]
83
83
> Learn more about how the semantic HTML/markup comparison in bUnit work, and how to customize it on the <xref:semantic-html-comparison> page.
84
84
85
-
### Passing parameters to components under test
85
+
### Passing Parameters to Components Under Test
86
86
87
87
Since we are declaring our component under test in Razor syntax, passing parameters to the component under test is exactly the same as passing parameters in normal Blazor components. This is the same for tests created with both the <xref:Bunit.Fixture> and <xref:Bunit.SnapshotTest> components.
88
88
@@ -92,7 +92,7 @@ In this example, we are passing both attribute parameters and child content to t
92
92
93
93
Injecting services into the components under test is covered on the <xref:inject-services-into-components> page.
94
94
95
-
## Further reading
95
+
## Further Reading
96
96
97
97
Now that we've covered the basics of writing tests using Razor syntax, you can continue digging deeper, e.g. by looking at:
Copy file name to clipboardExpand all lines: docs/docs/misc-test-tips.md
+5-5Lines changed: 5 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,13 @@
1
1
---
2
2
uid: misc-test-tips
3
-
title: Miscellaneous bUnit testing tips
3
+
title: Miscellaneous bUnit Testing Tips
4
4
---
5
5
6
-
# Miscellaneous bUnit testing tips
6
+
# Miscellaneous bUnit Testing Tips
7
7
8
8
Here is a few testing tips and tricks that have proven useful to us.
9
9
10
-
## Projects structure and tips and tricks
10
+
## Projects Structure and Tips and Tricks
11
11
12
12
The recommended solution/project structure for a test and production code project set-up is:
13
13
@@ -27,7 +27,7 @@ test
27
27
| SubComponent1Test.cs
28
28
```
29
29
30
-
## Use same root namespace and folder structure in both test- and production project
30
+
## Use same Root Namespace and Folder Structure
31
31
32
32
A neat trick, which will limit the `import` statements needed in your test project, is to set the root namespace to the same as that of the production code project, _AND_ use the same folder structure as shown above. Following the example above, the `MyComponentLibTests.csproj` file should contain:
33
33
@@ -37,7 +37,7 @@ A neat trick, which will limit the `import` statements needed in your test proje
0 commit comments