自動執行單元測試(NUnit)的簡單應用

來源:互聯網
上載者:User

學些了一段時間的單元測試(NUnit),對於小型的項目還用得馬馬虎虎,覺得在介面跑單元測試還是很容易上手的.但是最近深入瞭解了一些關於單元測試的理論,知道一個好的單元測試並不是靠手動去執行的,而是應該自動的執行.為了學得更加專業點,同時也想看看這個代碼的自動編譯是怎麼回事.最終牽涉到了NAnt.這個工具早已聽說過,就是沒有用過,還一直覺得用起來會很麻煩,畢竟用習慣了IDE.
簡單學習了一下NAnt,發現這個工具似乎與NUnit有著緊密的相聯,更覺得有趣的是NAnt以一種任務式的方式去執行項目編譯,還不會暫很大記憶體,畢竟還是命令列工具.比起IDE編譯時間,那種近似死機的狀態好(編譯大項目).Nant的任務都配置在一個XML檔案裡,只要簡單配置下,就可以執行N多項目的編譯,之後還可以順帶執行其他程式.也許有人會說,這不就是批處理.理論上說批處理也可以編譯N多檔案,麻煩而已.不過,Nant的功能不僅僅是編譯個項目,還附帶有很多函數,深入研究的話,當作一門語言來學也不為過.
當然好的單元測試,自動執行只是其中一個條件,另外更需要有合理的測試案例,將繼續學習.............

以下是*.build檔案的內容,自動編譯主專案和單元測試項目,最後自動執行單元測試.

*.build
 1<?xml version="1.0" encoding="utf-8"?>
 2<project name="MyProject" basedir="." default="run" xmlns="http://nant.sf.net/release/0.85/nant.xsd">
 3  <!--依次配置主專案檔案和測試專案的檔案,及單元測試檔案路徑-->
 4  <!--所有檔案路徑以檔案*.build所在路徑為根目錄-->
 5  <property name="MyProject.dir" value=".\MyProject"/>
 6  <property name="MyProject.bin" value="${MyProject.dir}\bin"/>
 7  <property name="MyProject.lib" value="${MyProject.dir}\lib"/>
 8  <property name="MyProjectTest.dir" value=".\MyProjectTest"/>
 9  <property name="MyProjectTest.bin" value="${MyProjectTest.dir}\bin"/>
10  <property name="MyProjectTest.lib" value="${MyProject.dir}\lib"/>
11  <property name="NUnit.exe" value=".\NUnit\nunit-console.exe"/>
12  <property name="MyProjectTest.nunit" value="MyProjectTest.nunit"/>
13  <!--執行任務-->
14  <target name="run">
15    <call target="BuildProject"/>
16    <call target="BuildProjectTest"/>
17    <call target="RunNunit"/>
18  </target>
19  <!--編譯主專案-->
20  <target name="BuildProject">
21    <echo message="Building MyProject"/>
22    <solution configuration="Debug" outputdir="${MyProject.bin}">
23      <projects>
24        <include name="${MyProject.dir}\MyProject.csproj"/>
25      </projects>
26      <assemblyfolders basedir="${MyProject.lib}"/>
27    </solution>
28  </target>
29  <!--編譯測試專案-->
30  <target name="BuildProjectTest">
31    <echo message="Building MyProjectTest"/>
32    <solution configuration="Debug" outputdir="${MyProjectTest.bin}">
33      <projects>
34        <include name="${MyProjectTest.dir}\MyProjectTest.csproj"/>
35      </projects>
36      <assemblyfolders basedir="${MyProjectTest.lib}"/>
37    </solution>
38  </target>
39  <!--執行單元測試-->
40  <target name="RunNunit">
41    <echo message="Executing Nuni Test"/>
42    <exec program="${NUnit.exe}">
43      <arg value="${MyProjectTest.nunit}"/>
44    </exec>
45  </target>
46</project>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.