Sigma Academy

Sigma Academy

  • Home
  • Học Tiếng Anh
  • Học tiếng Nhật
  • Luyện thi Ielts
  • Giáo Dục
  • Tin tức
    • Bất động sản
    • Phong Thuỷ
    • Công Nghệ
    • Ẩm thực
    • Làm Đẹp
You are here: Home / Công Nghệ / Unit testing C# with NUnit and .NET Core

Unit testing C# with NUnit and .NET Core

06/10/2023 06/10/2023 Sigma Academy

Video nunit testing

This tutorial takes you through an interactive experience building a sample solution step-by-step to learn unit testing concepts. If you prefer to follow the tutorial using a pre-built solution, view or download the sample code before you begin. For download instructions, see Samples and Tutorials.

Có thể bạn quan tâm
  • Tính toán và xử lý ngày tháng bằng câu lệnh SQL như thế nào?
  • Giới thiệu về Media CSS trong Responsive
  • Khắc Phục Lỗi Mạng VNPT Chậm, Thi Thoảng Lại Chập Chờn?
  • CCU là gì? – Họp Trực tuyến OnMeeting FPT Telecom
  • Thay mặt kính cảm ứng HTC One E9

This article is about testing a .NET Core project. If you’re testing an ASP.NET Core project, see Integration tests in ASP.NET Core.

Bạn đang xem: Unit testing C# with NUnit and .NET Core

Prerequisites

  • .NET Core 2.1 SDK or later versions.
  • A text editor or code editor of your choice.

Creating the source project

Open a shell window. Create a directory called unit-testing-using-nunit to hold the solution. Inside this new directory, run the following command to create a new solution file for the class library and the test project:

dotnet new sln

Next, create a PrimeService directory. The following outline shows the directory and file structure so far:

/unit-testing-using-nunit unit-testing-using-nunit.sln /PrimeService

Make PrimeService the current directory and run the following command to create the source project:

dotnet new classlib

Rename Class1.cs to PrimeService.cs. You create a failing implementation of the PrimeService class:

using System; namespace Prime.Services { public class PrimeService { public bool IsPrime(int candidate) { throw new NotImplementedException(“Please create a test first.”); } } }

Change the directory back to the unit-testing-using-nunit directory. Run the following command to add the class library project to the solution:

dotnet sln add PrimeService/PrimeService.csproj

Creating the test project

Next, create the PrimeService.Tests directory. The following outline shows the directory structure:

Xem thêm : Bài 1: Mạng GAN – mang đem lại sự sáng tạo cho trí tuệ nhân tạo!

/unit-testing-using-nunit unit-testing-using-nunit.sln /PrimeService Source Files PrimeService.csproj /PrimeService.Tests

Make the PrimeService.Tests directory the current directory and create a new project using the following command:

dotnet new nunit

The dotnet new command creates a test project that uses NUnit as the test library. The generated template configures the test runner in the PrimeService.Tests.csproj file:

<ItemGroup> <PackageReference Include=”nunit” Version=”3.13.3″ /> <PackageReference Include=”NUnit3TestAdapter” Version=”4.5.0″ /> <PackageReference Include=”Microsoft.NET.Test.Sdk” Version=”17.7.2″ /> </ItemGroup>

The test project requires other packages to create and run unit tests. The dotnet new command in the previous step added the Microsoft test SDK, the NUnit test framework, and the NUnit test adapter. Now, add the PrimeService class library as another dependency to the project. Use the dotnet add reference command:

dotnet add reference ../PrimeService/PrimeService.csproj

You can see the entire file in the samples repository on GitHub.

The following outline shows the final solution layout:

/unit-testing-using-nunit unit-testing-using-nunit.sln /PrimeService Source Files PrimeService.csproj /PrimeService.Tests Test Source Files PrimeService.Tests.csproj

Execute the following command in the unit-testing-using-nunit directory:

dotnet sln add ./PrimeService.Tests/PrimeService.Tests.csproj

Creating the first test

You write one failing test, make it pass, and then repeat the process. In the PrimeService.Tests directory, rename the UnitTest1.cs file to PrimeService_IsPrimeShould.cs and replace its entire contents with the following code:

Xem thêm : Callback là gì? (Callback trong javascript)

using NUnit.Framework; using Prime.Services; namespace Prime.UnitTests.Services { [TestFixture] public class PrimeService_IsPrimeShould { private PrimeService _primeService; [SetUp] public void SetUp() { _primeService = new PrimeService(); } [Test] public void IsPrime_InputIs1_ReturnFalse() { var result = _primeService.IsPrime(1); Assert.IsFalse(result, “1 should not be prime”); } } }

The [TestFixture] attribute denotes a class that contains unit tests. The [Test] attribute indicates a method is a test method.

Save this file and execute the dotnet test command to build the tests and the class library and run the tests. The NUnit test runner contains the program entry point to run your tests. dotnet test starts the test runner using the unit test project you’ve created.

Your test fails. You haven’t created the implementation yet. Make the test pass by writing the simplest code in the PrimeService class that works:

public bool IsPrime(int candidate) { if (candidate == 1) { return false; } throw new NotImplementedException(“Please create a test first.”); }

In the unit-testing-using-nunit directory, run dotnet test again. The dotnet test command runs a build for the PrimeService project and then for the PrimeService.Tests project. After you build both projects, it runs this single test. It passes.

Adding more features

Now that you’ve made one test pass, it’s time to write more. There are a few other simple cases for prime numbers: 0, -1. You could add new tests with the [Test] attribute, but that quickly becomes tedious. There are other NUnit attributes that enable you to write a suite of similar tests. A [TestCase] attribute is used to create a suite of tests that execute the same code but have different input arguments. You can use the [TestCase] attribute to specify values for those inputs.

Instead of creating new tests, apply this attribute to create a single data-driven test. The data driven test is a method that tests several values less than two, which is the lowest prime number:

[TestCase(-1)] [TestCase(0)] [TestCase(1)] public void IsPrime_ValuesLessThan2_ReturnFalse(int value) { var result = _primeService?.IsPrime(value); Assert.IsFalse(result, $”{value} should not be prime”); }

Run dotnet test, and two of these tests fail. To make all of the tests pass, change the if clause at the beginning of the Main method in the PrimeService.cs file:

if (candidate < 2)

Continue to iterate by adding more tests, theories, and code in the main library. You have the finished version of the tests and the complete implementation of the library.

You’ve built a small library and a set of unit tests for that library. You’ve also structured the solution so that adding new packages and tests is part of the standard workflow. You’ve concentrated most of your time and effort on solving the goals of the application.

Nguồn: https://sigma.edu.vn
Danh mục: Công Nghệ

Bài viết liên quan

Hóa Đại Cương – HIỆU ỨNG NHIỆT CỦA CÁC QUÁ TRÌNH HÓA HỌC
Tổng hợp đầy đủ font chữ biển số xe máy, ô tô 2023
Extension Marketplace
Extension Marketplace
Nội lực là gì? Điểm giống nhau giữa nội lực và ngoại lực là gì?
Hướng dẫn cách thay ổ cứng laptop đơn giản trong một nốt nhạc
Automation Test Là Gì? Kỹ Năng Cần Có Của Một Automation Tester
Automation Test Là Gì? Kỹ Năng Cần Có Của Một Automation Tester
Cách chuyển hình ảnh thành vector trong Illustrator (AI) nhanh chóng
Cách chuyển hình ảnh thành vector trong Illustrator (AI) nhanh chóng
TẠI SAO GỌI NƯỚC NGA LÀ XỨ SỞ BẠCH DƯƠNG?
Hướng dẫn thiết lập cài đặt gốc trên HTC One M8
PHÂN BIỆT BLACK BOX TEST VÀ WHITE BOX TEST, SƠ LƯỢC MỘT SỐ KỸ THUẬT TRONG BLACK BOX TEST
PHÂN BIỆT BLACK BOX TEST VÀ WHITE BOX TEST, SƠ LƯỢC MỘT SỐ KỸ THUẬT TRONG BLACK BOX TEST

Chuyên mục: Công Nghệ

About Sigma Academy

Previous Post: « Nghề lập trình Blockchain có thực sự phát triển? Những điều cần biết về Blockchain
Next Post: Ưu nhược điểm và kinh nghiệm khi mua nhà cũ »

Primary Sidebar

Bài viết nổi bật

Bảo vệ: Tổng hợp thông tin các ngành của Đại học Quốc gia Hà Nội

03/07/2024

TB Tuyển sinh Thạc Sĩ Chuyên ngành Khoa học Điều dưỡng – Khoá 3

27/06/2024

Thích ứng với chương trình lớp 10 mới: Đòi hỏi những thay đổi trong dạy và học

25/06/2024

Cập nhật nội dung & những thay đổi trong chương trình lớp 1 mới

Cập nhật nội dung & những thay đổi trong chương trình lớp 1 mới

24/06/2024

Giáo dục công dân lớp 9 – Giải bài tập sgk GDCD 9 ngắn nhất

24/06/2024

Các loại bằng thạc sĩ và cách phân biệt

24/06/2024

Giáo án Giáo dục địa phương lớp 6 năm 2023 – 2024 KHBD môn Giáo dục địa phương (Hà Nội, Hồ Chí Minh, Vĩnh Long, Thanh Hóa)

24/06/2024

[:vi]TOP CÁC TRƯỜNG ĐÀO TẠO NGÀNH KỸ THUẬT XÂY DỰNG CHẤT LƯỢNG[:]

[:vi]TOP CÁC TRƯỜNG ĐÀO TẠO NGÀNH KỸ THUẬT XÂY DỰNG CHẤT LƯỢNG[:]

24/06/2024

Thông báo tuyển sinh đào tạo Thạc sĩ Luật Khóa 37 (2023 - 2025)

Thông báo tuyển sinh đào tạo Thạc sĩ Luật Khóa 37 (2023 – 2025)

24/06/2024

Giải đáp về Chương trình Giáo dục Vinschool

24/06/2024

Ngành cơ điện tử: Học gì, học ở đâu và cơ hội nghề nghiệp

24/06/2024

3 nguyên tắc - 4 phương pháp giáo dục cảm xúc cho trẻ mầm non ba mẹ cần biết

3 nguyên tắc – 4 phương pháp giáo dục cảm xúc cho trẻ mầm non ba mẹ cần biết

24/06/2024

Ngành Logistics học trường nào sẽ dễ xin việc?

Ngành Logistics học trường nào sẽ dễ xin việc?

24/06/2024

Khoa Sau Đại học – Trường Đại học Mở Tp.HCM

24/06/2024

Chương trình liên kết quốc tế là gì? Hình thức liên kết phổ biến hiện nay

Chương trình liên kết quốc tế là gì? Hình thức liên kết phổ biến hiện nay

24/06/2024

Ngành An toàn thông tin

24/06/2024

Học thạc sĩ giáo dục tiểu học ở đâu? Điều kiện thi thạc sĩ giáo dục?

Học thạc sĩ giáo dục tiểu học ở đâu? Điều kiện thi thạc sĩ giáo dục?

24/06/2024

Ngành Digital Marketing học trường nào? Top 25+ trường đào tạo tốt nhất

Ngành Digital Marketing học trường nào? Top 25+ trường đào tạo tốt nhất

24/06/2024

Bộ GDĐT ban hành khung kế hoạch thời gian năm học 2022-2023

24/06/2024

3 nguyên tắc - 4 phương pháp giáo dục cảm xúc cho trẻ mầm non ba mẹ cần biết

3 nguyên tắc – 4 phương pháp giáo dục cảm xúc cho trẻ mầm non ba mẹ cần biết

24/06/2024

Footer

Về chúng tôi

Sigma Academy – sigma.edu.vn là tổ chức giáo dục kỹ năng cho trẻ. Đồng thời là thông tin tự động cập nhật Google chuyên cung cấp kiến thức về tất cả lĩnh vực. Website chúng tôi là web site cập nhật nội dung tự động từ google.com. Nếu có vấn đề gì về bản quyền vui lòng liên hệ: contact@sigma.edu.vn.

  • Điều khoản sử dụng
  • Chính sách bảo mật
  • Liên hệ

Mạng xã hội

  • Facebook
  • Zalo
  • Website
  • Google maps

Theo dõi chúng tôi tại Google News

Địa Chỉ

Trụ sở chính: 116/12 Tân Mỹ, phường Tân Thuận Tây, quận 7, Ho Chi Minh City, Vietnam
Điện thoại: 097.331.14.49 | Email: contact@sigma.edu.vn

Map

Bản quyền © 2025