Download NUnit
Download log4net
While installing remember where do you install them, since later on you would have to find the following 2 dlls:
log4net.dll ( for the using log4net; using log4net.Config; part )
and nunit.framework.dll ( for the using NUnit.Framework; part of the code )
Create a new project in VS ( or another IDE in your choice )
Add the references to the above mentioned dll's
Copy paste the bellow code. Build your solution.
Start the Nunit GUI. Create new Nunit project. Navigate to the bin folder of the project copy paste the path of the produced executable ( it contains also the tests in it - than press F5 to verify that all the tests are ran.
//this is the xml added replace here your log4net and Nunit paths //<Reference Include="log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821, processorArchitecture=MSIL"> // <SpecificVersion>False</SpecificVersion> // <HintPath>..\..\..\Log4Net\log4net-1.2.10\bin\net\2.0\release\log4net.dll</HintPath> //</Reference> //<Reference Include="nunit.framework, Version=2.4.8.0, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL" /> using System; using System.Collections.Generic; using System.Linq; using System.Text; using log4net; using log4net.Config; using NUnit.Framework; namespace NUnitSimple { class NUnitSubstractor { private static readonly ILog logger = LogManager.GetLogger ( typeof ( NUnitSubstractor ) ); public static void Substract ( int intToSusbractFrom , int intToSubstract , ref int intTheResult) { intTheResult = intToSusbractFrom - intToSubstract ; } static void Main ( string[] args ) { BasicConfigurator.Configure (); logger.Info ( " START " ); logger.Info ( " Hit a key to exit " ); Console.ReadLine (); } //eof method } //eof class [TestFixture]//telling NUnit that this class contains test functions public class TestNUnitSubstractor { [Test]//telling NUnit that this function should be run during the tests public void TestSubstractOk() { int intToSusbractFrom = 10 ; int intToSubstract = 4 ; int intTheResult = 0 ; NUnitSubstractor.Substract ( intToSusbractFrom , intToSubstract , ref intTheResult ) ; Assert.AreEqual ( 6 , intTheResult); } [Test]//telling NUnit that this function should be run during the tests public void TestSubstractNOK () { int intToSusbractFrom = 10; int intToSubstract = 4; int intTheResult = 0; NUnitSubstractor.Substract ( intToSusbractFrom, intToSubstract, ref intTheResult ); Assert.AreNotEqual ( 3, intTheResult ); } } //eof class } //eof namespace #region TheAppConfig /* <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSections> <log4net> <appender name="LogFileAppender" type="log4net.Appender.FileAppender"> <param name="File" value="LogTest2.txt" /> <param name="AppendToFile" value="true" /> <layout type="log4net.Layout.PatternLayout"> <param name="Header" value="[Header] \r\n" /> <param name="Footer" value="[Footer] \r\n" /> <param name="ConversionPattern" value="%d [%t] %-5p %c %m%n" /> </layout> </appender> <appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender"> <mapping> <level value="ERROR" /> <foreColor value="White" /> <backColor value="Red, HighIntensity" /> </mapping> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender"> <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.2.10.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> <connectionString value="data source=ysg;initial catalog=DBGA_DEV;integrated security=true;persist security info=True;" /> <commandText value="INSERT INTO [DBGA_DEV].[ga].[tb_Data_Log] ([Date],[Thread],[Level],[Logger],[Message]) VALUES (@log_date, @thread, @log_level, @logger, @message)" /> <parameter> <parameterName value="@log_date" /> <dbType value="DateTime" /> <layout type="log4net.Layout.PatternLayout" value="%date{yyyy'-'MM'-'dd HH':'mm':'ss'.'fff}" /> </parameter> <parameter> <parameterName value="@thread" /> <dbType value="String" /> <size value="255" /> <layout type="log4net.Layout.PatternLayout" value="%thread" /> </parameter> <parameter> <parameterName value="@log_level" /> <dbType value="String" /> <size value="50" /> <layout type="log4net.Layout.PatternLayout" value="%level" /> </parameter> <parameter> <parameterName value="@logger" /> <dbType value="String" /> <size value="255" /> <layout type="log4net.Layout.PatternLayout" value="%logger" /> </parameter> <parameter> <parameterName value="@message" /> <dbType value="String" /> <size value="4000" /> <layout type="log4net.Layout.PatternLayout" value="%messag2e" /> </parameter> </appender> <root> <level value="INFO" /> <appender-ref ref="LogFileAppender" /> <appender-ref ref="AdoNetAppender" /> <appender-ref ref="ColoredConsoleAppender" /> </root> </log4net> </configuration> */ #endregion TheAppconfig
No comments:
Post a Comment
- the first minus - Comments have to be moderated because of the spammers
- the second minus - I am very lazy at moderating comments ... hardly find time ...
- the third minus - Short links are no good for security ...
- The REAL PLUS : Any critic and positive feedback is better than none, so your comments will be published sooner or later !!!!