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 ?