如何写输出[ClassInitialize()]一个单元测试类的? [英] How to write output in the [ClassInitialize()] of a Unit Test class?

查看:683
本文介绍了如何写输出[ClassInitialize()]一个单元测试类的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写我的C#.NET应用程序的持久层的一些单元测试。前后测试类的测试执行,我希望做一些清理删除可能插入虚拟值,因此,这种清理在标有属性的方法发生了 [ClassInitialize()] [ClassCleanup()]



(我知道一个更好的办法是使用一个内存数据库,但它是不是真的可行,只要我们依靠大量的存储特效的....)



我想为输出有关结果的一些信息清理,但我不能找到一种方式来获得的测试结果与Visual Studio 2010 <输出/ p>

这是我到目前为止做的:

  /// ......很多东西之前... 

//全球测试运行
私有静态的TestContext背景;

//每个测试
私人IRepository回购;

#地区的初始化和清理

///<总结>
///前的测试套件
///<执行一次; /总结>
[ClassInitialize()]
公共静态无效InitTestSuite(的TestContext的TestContext)
{
=背景的TestContext;
removeTestDataFromDb();
}

[ClassCleanup()]
公共静态无效CleanupTestSuite()
{
removeTestDataFromDb();
}

私有静态无效removeTestDataFromDb()
{
context.WriteLine(removeTestDataFromDb启动);
使用(ISession的会话= NHibernateHelper.OpenSession())
{
的IDbConnection CN = session.Connection;
IDbCommand的CMD = cn.CreateCommand();
//删除anyt测试数据
cmd.CommandText = @DELETE FROM SomeTable
,其中somefield LIKE'%easyToFindTestData测试';
INT解析度= cmd.ExecuteNonQuery();
context.WriteLine(removeTestDataFromDb做 - 影响{0}行,RES);
}
}


[TestInitialize()]
公共无效InitTest()
{
回购=新MyRepositoryImplementation( );
}

[TestCleanup()]
公共无效CleanupTest()
{
//清理
回购= NULL;
}

#endregion



我试图用 context.WriteLine() ...



我也试过只使用 Console.WriteLine()与同结果。



你怎么写在 ClassInitialize 部分标准输出和在哪​​里可以访问该输出?


解决方案

[ClassInitialize] [ClassCleanup] 运行只有一次在该级所有测试。你会用更好的 [TestInitialize] [TestCleanUp] 这和之前每次测试之后运行。也可以尝试在一个数据库事务包装完整的测试。这种方式可以简单地回滚操作(通过不提交事务)和数据库保持一致的状态(这是值得信赖的自动化测试非常必要)。



一个绝招我的集成测试做的就是定义一个基类,我所有的集成测试类可以继承。基类确保每个测试是在一个交易,这一交易被回滚跑去。下面是代码:

 公共抽象类IntegrationTestBase 
{
私人TransactionScope的范围;

[TestInitialize]
公共无效TestInitialize()
{
=范围TransactionScope的新();
}

[TestCleanup]
公共无效TestCleanup()
{
scope.Dispose();
}
}



祝你好运。


I am writing some unit tests for the persistence layer of my C#.NET application. Before and after the tests of a test class execute, I want to do some cleaning up to erase possibly inserted dummy values, therefore, this cleaning up happens in methods marked with the attributes [ClassInitialize()] and [ClassCleanup()].

(I know that a better way would be to use an in-memory database, but it is not really doable so far as we depend on lots of stored procs....)

I would like to output some information about the results of the cleaning up, but I can not find a way to get the output in the test results with VISUAL Studio 2010.

This is what I am doing so far :

        ///... lots of stuff before ...

        //global for the test run
        private static TestContext context;

        //for each test
        private IRepository repo;

        #region Initialisation and cleanup

        /// <summary>
        /// Execute once before the test-suite
        /// </summary>
        [ClassInitialize()]
        public static void InitTestSuite(TestContext testContext)
        {
            context = testContext;    
            removeTestDataFromDb();    
        }

        [ClassCleanup()]
        public static void CleanupTestSuite()
        {
            removeTestDataFromDb();
        }

        private static void removeTestDataFromDb()
        {
            context.WriteLine("removeTestDataFromDb starting");
            using (ISession session = NHibernateHelper.OpenSession())
            {    
                IDbConnection cn = session.Connection;
                IDbCommand cmd = cn.CreateCommand();
                //remove anyt test data
                cmd.CommandText = @"DELETE FROM SomeTable
                    WHERE somefield LIKE 'easyToFindTestData%Test'";
                int res = cmd.ExecuteNonQuery();    
                context.WriteLine("removeTestDataFromDb done - affected {0} rows", res);
            }
        }


        [TestInitialize()]
        public void InitTest()
        {
            repo = new MyRepositoryImplementation();
        }

        [TestCleanup()]
        public void CleanupTest()
        {
            //cleanup       
            repo = null;
        }

        #endregion

I'm trying to use context.WriteLine() ...

I also tried just using Console.WriteLine() with the same results.

How do you write to standard output in the ClassInitialize part and where can you access that output ?

解决方案

The [ClassInitialize] and [ClassCleanup] run just once for all the tests in that class. You'd be better of using [TestInitialize] and [TestCleanUp] which run before and after each test. Also try wrapping the complete test in a database transaction. This way you can simply rollback the operation (by not committing the transaction) and your database stays in a consistent state (which is essential for trustworthy automated tests).

A trick I do for integration tests is to define a base class that all my integration test classes can inherit from. The base class ensures that each test is ran in a transaction and that this transaction is rolled back. Here is the code:

public abstract class IntegrationTestBase
{
    private TransactionScope scope;

    [TestInitialize]
    public void TestInitialize()
    {
        scope = new TransactionScope();
    }

    [TestCleanup]
    public void TestCleanup()
    {
        scope.Dispose();
    }
}

Good luck.

这篇关于如何写输出[ClassInitialize()]一个单元测试类的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆