【发布时间】:2010-12-07 07:44:37
【问题描述】:
我正在为我的 C#.NET 应用程序的持久层编写一些单元测试。在测试类的测试执行之前和之后,我想做一些清理以擦除可能插入的虚拟值,因此,这种清理发生在标有[ClassInitialize()] 和@987654322 属性的方法中@。
(我知道更好的方法是使用内存数据库,但就我们依赖大量存储的过程而言,这并不是真正可行的......) p>
我想输出一些关于清理结果的信息,但我找不到使用 VISUAL Studio 2010 在测试结果中获取输出的方法。
这就是我目前正在做的事情:
///... 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
我正在尝试使用 context.WriteLine() ...
我也尝试使用 Console.WriteLine() 获得相同的结果。
您如何在ClassInitialize 部分中写入标准输出以及在哪里可以访问该输出?
【问题讨论】:
标签: c# unit-testing visual-studio-2010