[[oktatas:programozás:csharp|< Csharp]] ====== NUnit használata ====== ===== Beszerzés ===== Windowsra, Linuxra http://nunit.org weboldalról tölthetjük le. Linuxon a csomagkezelővel is telepíthetjük: apt install nunit Windowson nuget csomagkezelővel: nuget install NUnit A kimenet szerint a következő helyre telepszik: C:\Windows\system32. Keressük itt a 3.13.2 verziót: * C:\Windows\System32\NUnit.3.13.2> Telepítsünk futtatót choco paranccsal: choco install nunit-console-runner Kapunk egy ilyen programot: nunit3-console ===== Háromszögpélda ===== using System; class Haromszog { public static double SzamitTerulet(double alap, double magassag) { return (alap * magassag) / 2; } public static void Main() { Console.WriteLine("Hi"); } } using NUnit.Framework; [TestFixture] public class HaromszogTest { [Test] public void TesztSzamitTerulet() { Assert.AreEqual(525, Haromszog.SzamitTerulet(30, 35)); } } # Linuxos Makefile all: dmcs Haromszog.cs NUNITL=/usr/lib/cli/nunit.framework-2.6/nunit.framework.dll testl: dmcs -r:$(NUNITL) /t:library /out:HaromszogTeszt.dll Haromszog.cs HaromszogTeszt.cs nunit-console HaromszogTeszt.dll NUNITW=c:\Program Files\NUnit 2.6.3\bin\framework\nunit.framework.dll testw: csc -r:$(NUNITW) /t:library /out:HaromszogTeszt.dll Haromszog.cs HaromszogTeszt.cs nunit-console HaromszogTeszt.dll ===== Háromszög példa kivételköveteléssel ===== using System; class Haromszog { public static double szamitTerulet(double alap, double magassag) { if(alap <= 0 || magassag <= 0) { throw new ArgumentException(); } return (alap * magassag) / 2; } public static void Main() { Console.WriteLine("Hi"); } } Az ArgumentException kivétel a System névtérben van, ezért használatba vesszük. using NUnit.Framework; using System; [TestFixture] public class HaromszogTest { [Test] public void TesztSzamitTerulet() { Assert.AreEqual(525, Haromszog.szamitTerulet(30, 35)); } [Test] [ExpectedException(typeof(ArgumentException))] public void TesztExceptionSzamitTerulet() { Haromszog.szamitTerulet(0, 35); } } # Linuxos Makefile FRAMEWORK=/usr/lib/cli/nunit.framework-2.6/nunit.framework.dll all: dmcs Haromszog.cs test: dmcs -r:$(FRAMEWORK) /t:library /out:HaromszogTeszt.dll Haromszog.cs HaromszogTeszt.cs nunit-console HaromszogTeszt.dll ===== Linkek ===== * http://www.josenet.com/2010/02/how-to-make-nunit-work-with-visual.html * [[https://github.com/nunit/nunit-vs-adapter/wiki/NUnit-Test-Adapter-for-Visual-Studio-2012-and-2013|https://github.com/]] * [[http://blogs.msdn.com/b/africaapps/archive/2013/02/26/test-driven-development-using-nunit-natively-in-visual-studio-2012.aspx|http://blogs.msdn.com/]]