单元测试从另一个单元测试中调用的方法 [英] Unit test a method which is being called from another unit test

查看:136
本文介绍了单元测试从另一个单元测试中调用的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





有人可以帮助我如何对从另一个单元调用的方法进行单元测试测试



以下是示例代码

公共类订单

   {  

     public bool SaveOrder(OrderInfo orderInfo)

     {  

         float
discountAmt = GetDiscount(orderInfo);


         return true;

    }



     public float GetDiscount(OrderInfo orderInfo)

     {

       //基于orderInfo.couponCode,应用折扣

       return 0;

    }



  }



这是我的单元测试用例

  [测试]

  public void SaveOrderTest()

     {

      订单orderObj = new Order();

       OrderInfo orderInfo = new OrderInfo();

       orderInfo.couponCode =" Save20";

       orderInfo.orderAmount =" 15000";

       orderInfo.orderID =" 3";

       Assert.AreEqual(true,orderObj.SaveOrder(orderInfo));



    }



  [测试]

  public void GetDiscountTest()

     {

      订单orderObj = new Order();

       OrderInfo orderInfo = new OrderInfo();

       orderInfo.couponCode =" Save20";

       orderInfo.orderAmount =" 15000";

       orderInfo.orderID =" 3";

       Assert.AreEqual(20,orderObj.GetDiscount(orderInfo));



    }





这里,SaveOrderTest()在内部调用GetDiscount(),它有一个单独的单元测试方法。当我查看单元测试报告时,它说
SaveOrderTest()被传递但是还有另一个也传递了一个名为GetDiscount()的方法,该方法具有单独的单位。当我们运行父测试用例时,有没有办法获得所有子测试用例的结果?




问题1:

上面提到的代码只是一个例子,但在实际场景中我们有多种方法可以定义测试用例。
所以这迫使我们从单元测试中调用相同的方法以及来自父方法。 




问题2:

有没有办法有条件地运行测试用例,在上面的例子中,我们想在GetDiscount()中发生代码更改时运行SaveOrderTest()。
因为当前它正在执行代码构建后的所有测试用例需要很长时间,而我们希望执行因当前代码更改或构建而受影响的测试用例。

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.

推荐答案

>>当我们运行父测试用例时,有没有办法获得所有子测试用例的结果?

不确定您是否正确理解了您的问题,测试结果取决于您调用测试方法的时间长度[TestMethod]。

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.

>>问题2:

有没有办法有条件地运行测试用例,在上面的例子中,我们想在GetDiscount()中有代码更改
时运行SaveOrderTest()。目前是becuase它正在执行代码构建后执行所有测试用例,这需要花费很长时间,而我们希望执行因当前代码更改或构建而受影响的测试用例。

>>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.

构建后运行测试是Visual Studio中的一个选项,您可以将其禁用。在构建测试项目之后,您可以在Test Explorer中看到测试方法,以及您可以通过右键单击其名称来运行特定的测试方法。

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.

 

问候,

弗莱彻


这篇关于单元测试从另一个单元测试中调用的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆