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
  • Quản trị mạng là gì? Mô tả công việc, kỹ năng và trường đào tạo
  • Cách chạy file PHP trên XAMPP
  • Cây vọng cách, rau vọng cách
  • Cách Jailbreak IPhone XR đến Iphone 11 Pro Max bằng Unc0ver 4.0
  • Tổng hợp 17 trang web thay thế KeepVid để tải nhạc, video YouTube

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 : Tổng hợp câu lệnh Linux: Cài đặt ứng dụng

/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 : Cách sửa lỗi font chữ khi copy từ PDF sang Word

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

100+ CÂU TIẾNG ANH GIAO TIẾP HÀNG NGÀY THÔNG DỤNG NHẤT

100+ CÂU TIẾNG ANH GIAO TIẾP HÀNG NGÀY THÔNG DỤNG NHẤT

23/11/2023

Trắc nghiệm tiếng Anh 11 Unit 1 A long and healthy life Trắc nghiệm tiếng Anh 11 – Global Success

23/11/2023

Bí quyết xử lý gọn tất cả các bài thi nghe tiếng Anh

Bí quyết xử lý gọn tất cả các bài thi nghe tiếng Anh

23/11/2023

(no title)

23/11/2023

121 bài tập Tiếng Anh 6 thí điểm

23/11/2023

Tổng hợp từ vựng tiếng Anh bắt đầu bằng chữ m thông dụng bạn cần biết

Tổng hợp từ vựng tiếng Anh bắt đầu bằng chữ m thông dụng bạn cần biết

23/11/2023

50 câu giao tiếp tiếng Anh cơ bản cho người mất gốc

50 câu giao tiếp tiếng Anh cơ bản cho người mất gốc

23/11/2023

TỪ VỰNG TIẾNG ANH CHỦ ĐỀ CON VẬT CHO BÉ

TỪ VỰNG TIẾNG ANH CHỦ ĐỀ CON VẬT CHO BÉ

23/11/2023

Mẫu câu tiếng Anh giao tiếp nói chuyện với người nước ngoài thông dụng nhất

Mẫu câu tiếng Anh giao tiếp nói chuyện với người nước ngoài thông dụng nhất

23/11/2023

Top 6 web học từ vựng tiếng Anh miễn phí

Top 6 web học từ vựng tiếng Anh miễn phí

23/11/2023

TIN TỨC

23/11/2023

48 Câu bài tập phát âm tiếng Anh lớp 5 giúp bé luyện nói hiệu quả

23/11/2023

[100+] Từ Vựng Tiếng Anh Về Nghề Nghiệp Cho Trẻ (2023)

[100+] Từ Vựng Tiếng Anh Về Nghề Nghiệp Cho Trẻ (2023)

23/11/2023

Học tiếng Anh giao tiếp online với người nước ngoài

Học tiếng Anh giao tiếp online với người nước ngoài

23/11/2023

Direct English

Direct English

23/11/2023

Từ vựng tiếng Anh trình độ A1 A2 theo chủ đề PDF

Từ vựng tiếng Anh trình độ A1 A2 theo chủ đề PDF

23/11/2023

A-Z các quy tắc dạy trẻ đánh vần tiếng Anh hiệu quả

A-Z các quy tắc dạy trẻ đánh vần tiếng Anh hiệu quả

23/11/2023

(no title)

23/11/2023

Tổng hợp trọn bộ tài liệu học từ vựng IELTS dành cho người mới bắt đầu

Tổng hợp trọn bộ tài liệu học từ vựng IELTS dành cho người mới bắt đầu

22/11/2023

Top 7 Website Giúp Làm Bài Tập Tiếng Anh Online

Top 7 Website Giúp Làm Bài Tập Tiếng Anh Online

22/11/2023

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 © 2023