如何创建一个简单的示例控制台应用程序,用于为测试驱动开发开发单独的类?

时间:2009-05-01 07:04:14

标签: c# unit-testing logging nunit log4net

我打算创建一个简单易于调试的控制台应用程序,它将成为C#中测试驱动开发单个类的模板(或起点)。目的是有一个简单的文件夹,这个控制台应用程序将驻留在该文件夹中,只需复制粘贴文件夹打开新项目并开始编写新类。一旦所有功能都经过测试(最好是测试应该在同一个文件中(或者至少是命名空间),这个类将被允许进入更大的项目。我使用的是NUnit和log4net。如果你这样做的话“小型测试驱动单元构建”方法如何实现它。请发布一些代码或解释。如果您不使用,请提供解释原因? 这是代码(配置数据也粘贴为注释......)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using log4net.Config;
using NUnit.Framework;

namespace NUnitSimple
{

  class TheClassToTest_Substractor
  {

    private static readonly ILog logger =
         LogManager.GetLogger ( typeof ( TheClassToTest_Substractor ) );



    public static void Substract ( int intToSusbractFrom , int intToSubstract , ref int intTheResult)
    {
     intTheResult = intToSusbractFrom - intToSubstract ; 
    }


    static void Main ( string[] args )
    {
      DOMConfigurator.Configure (); //tis configures the logger 


      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 TestTheClassToTest_Substractor
{ 

     [Test]//telling NUnit that this function should be run during the tests 
    public void TestSubstractOk() 
    { 
       int intToSusbractFrom = 10 ; 
       int intToSubstract = 4 ; 
        int intTheResult = 0 ;
       TheClassToTest_Substractor.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;
       TheClassToTest_Substractor.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="Program.log" />
      <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

#region TheXmlReferingToTheNUnitAndLog4NetInNUnitSimple.csprojFile
/*
     <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" />
 */
#endregion

2 个答案:

答案 0 :(得分:2)

为什么不查看像Tree Surgeon这样的脚本工具来设置项目结构,而不是设置项目然后剪切和粘贴。这将设置您的解决方案,项目和项目文件夹。它包括nUnit和nAnt的功能。

请务必查看项目主页底部镜像的博文。

BTW:我同意Jon Skeet的控制台应用程序。它们可能很有用,但如果你有VS.2005及更早版本的TestDriven.Net工具或VS.2008测试工具,你可以在不创建自己的线束应用程序的情况下进入测试代码。

答案 1 :(得分:1)

我真的没有看到在现有项目中包含新类的好处,包括现有测试项目中的新测试类,以及在那里运行测试。

为什么要推出一款控制台应用?您是否真的需要从控制台应用程序而不是现有的测试运行程序中运行测试? (不要误解我的意思,我是控制台应用程序的忠实粉丝 - 我只是不知道它们是如何适应的。)