none
Get dynamic data from both TestInitialize and TestCleanup methods RRS feed

  • Question

  • I have a DynamicData test which is based on: https://www.meziantou.net/mstest-v2-data-tests.htm#using-dynamicdata

    In my own case, I have a Pre Test (TestInitialize) and Post-Test (TestCleanup), and I need to get the dynamic data value from that pre-test and post-test. I mean, I need to get the the value of the arguments "a", "b" and "expected" from both TestInitialize and TestCleanup methods.

    Here is a sample code:

    [DataTestMethod]
    [DynamicData(nameof(GetData), DynamicDataSourceType.Method)]
    public void Test_Add_DynamicData_Method(int a, int b, int expected)
    {
        var actual = MathHelper.Add(a, b);
        Assert.AreEqual(expected, actual);
    }
    
    public static IEnumerable<object[]> GetData()
    {
        yield return new object[] { 1, 1, 2 };
        yield return new object[] { 12, 30, 42 };
        yield return new object[] { 14, 1, 15 };
    }
    
    [TestInitialize]
    public virtual void PreTest()
    {
               
    }
    
    [TestCleanup]
    public virtual void PostTest()
    {
                
    }

    Can you please assist?

    Best Regards,
    Nadeem Bader

    Thursday, July 18, 2019 4:35 PM

Answers

  • Hi Nadim2522,

    Thank you for reply.

    The execution of unit test is TestInitial -> TestMethod -> TestCleanup, they are opposite each other. And they could only execute methods with no return value. And it is bu design. So I'm afraid that there is no appropriate method to implement this function.

    So if you want to call data for your TestMethod, please try to use data source to connect database, for example:

    [DataSource(@"Provider=Microsoft.SqlServerCe.Client.4.0; Data Source=C:\Data\MathsData.sdf;", "Numbers")]

    Please refer more: How to: Create a data-driven unit test

    And you could use code to set the data into excel file(or other database file), then get/set from this file during TestInitial,TestMethod and Testcleanup.

    Hope it could help you.

    Best Regards,

    Dylan


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    • Marked as answer by Nadim2522 Saturday, July 27, 2019 3:59 PM
    Friday, July 26, 2019 8:32 AM

All replies

  • Hi Nadim2522, 

    Thank you for posting here.

    According to your description, your issue is more related to Visual Studio Unit Testing. Therefore, it will be more appropriate to ask your question in Visual Studio Unit Testing Forums.

    The C# forum is for C# related question, we discuss and ask the C# programming language, IDE, libraries, samples and tools here.

    Thank you for your understanding.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, July 19, 2019 6:46 AM
  • Hi Nadim2522,

    Sorry for delay in reply.

    For your requirement, I am afraid that the answer is negative. After my experiments,the method under TestInitialize and TestCleanup could not return values, so the objects could not be passed out them and be called by other test methods.

    Any feedback will be expected.

    Best Regards,

    Dylan


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    Monday, July 22, 2019 10:48 AM
  • Hi

    Thank you for your reply

    Is there any workaround that could be done in this case?

    Best Regards,
    Nadeem Bader 

    Wednesday, July 24, 2019 8:15 AM
  • Hi Nadim2522,

    Thank you for reply.

    The execution of unit test is TestInitial -> TestMethod -> TestCleanup, they are opposite each other. And they could only execute methods with no return value. And it is bu design. So I'm afraid that there is no appropriate method to implement this function.

    So if you want to call data for your TestMethod, please try to use data source to connect database, for example:

    [DataSource(@"Provider=Microsoft.SqlServerCe.Client.4.0; Data Source=C:\Data\MathsData.sdf;", "Numbers")]

    Please refer more: How to: Create a data-driven unit test

    And you could use code to set the data into excel file(or other database file), then get/set from this file during TestInitial,TestMethod and Testcleanup.

    Hope it could help you.

    Best Regards,

    Dylan


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    • Marked as answer by Nadim2522 Saturday, July 27, 2019 3:59 PM
    Friday, July 26, 2019 8:32 AM
  • Hi,

    Thank you Dylan for the suggestion - it could work

    Best Regards,
    Nadeem Bader

    Saturday, July 27, 2019 4:00 PM