none
Unit test a method which is being called from another unit test RRS feed

  • Question

  • Hi,

    Could some one help me on how to Unit test a method which is being called from another unit test

    Here is the sample Code
    public class Order
        {   
            public bool SaveOrder(OrderInfo orderInfo)
            {   
                  float discountAmt = GetDiscount(orderInfo);
                    return true;
            }

            public float GetDiscount(OrderInfo orderInfo)
            {
                //based on orderInfo.couponCode, apply discount
                return 0;
            }

        }

    this is My unit test case
     [Test]
     public void SaveOrderTest()
            {
                Order orderObj = new Order();
                OrderInfo orderInfo = new OrderInfo();
                orderInfo.couponCode = "Save20";
                orderInfo.orderAmount = "15000";
                orderInfo.orderID = "3";
                Assert.AreEqual(true, orderObj.SaveOrder(orderInfo));

            }

     [Test]
     public void GetDiscountTest()
            {
                Order orderObj = new Order();
                OrderInfo orderInfo = new OrderInfo();
                orderInfo.couponCode = "Save20";
                orderInfo.orderAmount = "15000";
                orderInfo.orderID = "3";
                Assert.AreEqual(20, orderObj.GetDiscount(orderInfo));

            }


    Here, SaveOrderTest() is internally calling GetDiscount() which has a seperate unit test method. When I look at the unit test report, it says SaveOrderTest() is passed but there is another method called GetDiscount() which has seperate unit is also passed. Is there a way to get results of all child test cases when we run parent test case?

    Question 1:
    The above mentioned code is just an example but in actual scenario we have mutiple levels of methods which alredy have test cases defined. So this is forcing us to call the same method from unit test as well as from parent method. 

    Question 2:
    Is there a way to run test cases conditionally, in the above case, we would like to run SaveOrderTest() when there is a code change in GetDiscount(). Becuase currently it is executing all the test cases after the code build which is taking long time, instead we would like to execute the test cases which are impacted due to current  code change or build.
    Tuesday, April 24, 2018 12:18 PM

All replies

  • >>Is there a way to get results of all child test cases when we run parent test case?

    Not sure if you understand your question correctly, the test result depends how many [TestMethod] you have other than how many time you call the test method.

    >>Question 2:
    Is there a way to run test cases conditionally, in the above case, we would like to run SaveOrderTest() when there is a code change in GetDiscount(). Becuase currently it is executing all the test cases after the code build which is taking long time, instead we would like to execute the test cases which are impacted due to current  code change or build.

    Run test after build is an option in Visual Studio, you could just disable it. After build test project, you could see the test methods you have in the Test Explorer, and you can run the specific test method by right click its name.

     

    Regards,

    Fletcher


    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.


    Wednesday, April 25, 2018 8:41 AM