locked
Write output from AssemblyInitialize to a separate file RRS feed

  • Question

  • i am building a test harness to do (mostly regression) tests on a set of custom application types. I am using the Visual studio unit testing tools as the mechanism to build and run the tests.

    I reference and use a Library that contains a number of classes and  helper methods, here i am calling it MyTestHarness

    and for the sake of this question, the MyTestHarness namespace contains the MYTesting class.

    the code looks something like this:

    ...

    using MyTestHarness; using Microsoft.VisualStudio.TestTools.UnitTesting; ... namespace UnitTestProject1 { [TestClass] public class UnitTest1 { public TestContext TestContext { get; set; } [AssemblyInitialize] public static void Testsetup(TestContext Context) { MyTesting.SetupTest(Context, Some Input args); }


    then it contains some methods decorated with the [TestMethod] attribute, that do some tests.

    finally i have cleanup

     
    [AssemblyCleanup]
            public static void TestCleanup()
            {
                MyTesting.CleanupTest();
            }

    inside the setup and cleanup methods i have a decent amount of logging, to let the user more easily find the source of errors.  

    This all works, and the test output shows all the logging.

    the problem i have is simple:

    How do i write a separate file that contains the setup or cleanup logging only? currently, the output file of whichever TestMethod happens to run first gets all the logging for the setup in it. However, i would like to write the logging for the setup to a different file(preferable one i specify) and it still needs to get written out to the test results directory.

    I have looked through the MSDN library, and goggled, but to no avail; Am i missing something simple? or is this actually not well documented? 

    Any help here would be most appreciated.

    EDIT:Also, i forgot to mention that i am using .net 4.5 framework and visual studio 2012 professional.
    • Edited by Magnus_genesis Saturday, May 11, 2013 6:39 AM additional info included
    Saturday, May 11, 2013 6:37 AM

Answers

  • figured itout.

    In case someone else needs to do the same thing, here is how i did it.

    Create a file( i used a File stream), using the TestContext Directory structure.

    string filePath = Path.Combine(TestContext.TestLogsDir, "Setup.txt");
    FileStream stream = new FileStream(filePath, FileMode.Create);

    ... write whatever you want in the file...

    then you add the file the the test output.

    like this.

    TestContext.AddResultFile(filePath);

    Done.



    Wednesday, May 15, 2013 5:09 AM

All replies

  • Hello,

    Thank you for your post.

    AssemblyInitializeAttribute Class identifies a method that contains code to be used before all tests in the assembly have run and to allocate resources obtained by the assembly. The method marked with AssemblyInitialize attribute will be run before methods marked with the ClassInitializeAttribute, TestInitializeAttribute, and TestMethodAttribute attributes.

    I am not very sure about what your setup and cleanup logging is and what logging you are getting now. If possible, you can provide us an example.

    And if you want to write the logging for the setup to a different file, I am afraid that it should be an issue on coding rather than test itself. You should develop your own task to write logging to an expected file and the save it into test result directory.

    Best regards,


    Amanda Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Edited by Amanda Zhu Monday, May 13, 2013 5:37 AM
    Monday, May 13, 2013 5:36 AM
  • my logging is only a method class to flexibly use TestContext.WriteLine() using a single call from anywhere in my test harness.
    Tuesday, May 14, 2013 7:06 PM
  • figured itout.

    In case someone else needs to do the same thing, here is how i did it.

    Create a file( i used a File stream), using the TestContext Directory structure.

    string filePath = Path.Combine(TestContext.TestLogsDir, "Setup.txt");
    FileStream stream = new FileStream(filePath, FileMode.Create);

    ... write whatever you want in the file...

    then you add the file the the test output.

    like this.

    TestContext.AddResultFile(filePath);

    Done.



    Wednesday, May 15, 2013 5:09 AM