<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Test on</title><link>https://tonny0531.github.io/categories/test/</link><description>Recent content in Test on</description><generator>Hugo -- gohugo.io</generator><lastBuildDate>Sun, 09 Apr 2023 13:55:48 +0900</lastBuildDate><atom:link href="https://tonny0531.github.io/categories/test/index.xml" rel="self" type="application/rss+xml"/><item><title>NUnit Test Project Init</title><link>https://tonny0531.github.io/post/dotnet/test/init/</link><pubDate>Sun, 09 Apr 2023 13:55:48 +0900</pubDate><guid>https://tonny0531.github.io/post/dotnet/test/init/</guid><description>前言 前面已經有寫了幾篇關於解耦合，抽象行為等等概念的說明了．
而建置測試專案雖然已經做了第二次了，但發現兩次我都會忘記怎麼做，因此才特別寫了這篇初始化的文章出來
碰到問題 在.NET Core 開發的過程中，都知道有IServiceProvider和IConfiguration這兩個介面可以取得實例或是Config的配置
但正常情況下都被框架包好了，根本不用去考慮他怎麼做出來的．
但在測試的途中，框架不會知道你是否要使用，因此也不會自動幫你建立一個出來，導致我們需要自己來建立．
建立專案 由於我現在的開發環境是 Mac OS 因此這裡就只示範 VSCode &amp;amp; .NET CLI
dotnet new nunit -n {ProjectName} 如果是6版本以上的話，會自動建立using.cs
這個是GloableUsing的部分，如果有全域要使用的using的話可以考慮放到這裡
Nunit的介紹 正常來講在寫整合測試的時候，我們為了減少變因，因此都會做一次初始化以及完畢的行為
例如：初始化建立相依的物件(DB,Redis等等相關連線)
完畢的時候要刪除測試的資料等等行為
而在NUnit中的屬性則叫做Setup(初始) &amp;amp; TearDown(結束)
實際上寫出來會長得像這樣
[SetUp] public void SetUp() { //初始化區塊 } [TearDown] public void TearDown() { //測試完成後執行的區塊 } 建立IServiceProvider &amp;amp; IConfiguration 建立方式其實沒有很困難，實際方式如下
protected IServiceCollection _services; protected IServiceProvider _sp; protected IConfiguration _configuration; [SetUp] public void SetUp() { // 先建立新的ServiceCollection this.</description></item></channel></rss>