Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/barisates/dotnet-core-xunit-example
Unit Test in .NET Core Web Api with xUnit
https://github.com/barisates/dotnet-core-xunit-example
dotnet-core functional-testing unit-testing web-api xunit
Last synced: 2 days ago
JSON representation
Unit Test in .NET Core Web Api with xUnit
- Host: GitHub
- URL: https://github.com/barisates/dotnet-core-xunit-example
- Owner: barisates
- Created: 2019-11-25T20:01:31.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2022-07-06T06:20:08.000Z (over 2 years ago)
- Last Synced: 2023-03-02T23:22:15.697Z (over 1 year ago)
- Topics: dotnet-core, functional-testing, unit-testing, web-api, xunit
- Language: C#
- Size: 26.4 KB
- Stars: 16
- Watchers: 1
- Forks: 9
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# dotnet-core-xunit-example
**Unit Test in .NET Core Web Api with xUnit.**In this example, we have a simple Web Api developed with .Net Core which performs database operations. Our Web API consists of the following endpoints;
> `UserDto.User GetUser(int id);` get user with id in database
> `UserDto.User AddUser(UserDto.User user);` add user to database
> `UserDto.User DeleteUser(int id;` delete user with id from database
When testing these endpoints, we actually write **functional tests**. In this way, we can manage test processes more realistically.
Our tests use **memory database** when testing our endpoints and our live data is not affected by the testing process.
When developing a test, we use the library **[xUnit.net](https://github.com/xunit/xunit "xUnit.net")**.
### xUnit
xUnit is an open-source unit testing tool for the .Net Framework and offers **.NET Core support**. Compared to other unit testing frameworks, it stands out with its ease of development and its approach to behaviors like *SetUp, TearDown, OneTimeSetup*.
**[Comparing xUnit.net to other frameworks.](https://xunit.net/docs/comparisons.html "Comparing xUnit.net to other frameworks.")**
**SetUp (before each test)**
XUnit uses constructors for test setup operations. You don't need to use a separate attirubute as in NUnit, MSTest frameworks.**TearDown (after each test)**
XUnit uses IDisposable classes for teardown operations.**Implementing SetUp and Teardown Method in XUnit;**
```csharp
public class TruthTests : IDisposable
{
public TruthTests()
{
// It will work before each test.
// NUnit: [SetUp]
// MSTest: [TestInitialize]
}
public void Dispose()
{
// It will work after each test.
// NUnit: [TearDown]
// MSTest: [TestCleanup]
}[Fact]
public void Test1()
{
// Your Test
}
}
```**OneTimeSetup (share context between tests)**
We use xUnit's IClassFixture feature to create shared contexts, such as Databases. With the fixture, we can share a single object instance between all tests.
For more; [xUnit.net Documentation](https://xunit.net/#documentation "xUnit.net Documentation")
- [Running Tests in Parallel](https://xunit.net/docs/running-tests-in-parallel "Running Tests in Parallel")
- [Shared Context between Tests](https://xunit.net/docs/shared-context "Shared Context between Tests")