简体   繁体   English

如何在场景中测试多个方法调用一个辅助方法

[英]How to test in scenario many methods calling one helper method

For example here is my scenario:例如,这是我的场景:

function A() {
   C();
}

function B() {
   C();
}

function C() {
   if (someState > 0) then doSomething();
   else doSomethingElse();
}

I want to make all test case that coverage all code.我想让所有测试用例覆盖所有代码。 Because C() has a condition so for testing C() , we need two testing method: testC1() and testC2() .因为C()有一个条件,所以为了测试C() ,我们需要两种测试方法: testC1()testC2() So the total test is: testA_C1() testA_C2() testB_C1() testB_C1() .所以总测试是: testA_C1() testA_C2() testB_C1() testB_C1() Number of testing methods will be increased dramatically when there are more conditions, and there are more methods that use same method C()条件越多,测试方法的数量就会急剧增加,并且使用相同方法的方法越多C()

The problem here is: C() is not depend on any state of A() and B() , so in fact I think C() can be tested separately.这里的问题是: C()不依赖于A()B()任何状态,所以实际上我认为C()可以单独测试。 So I think we can save a big amount of unit test.所以我认为我们可以节省大量的单元测试。

My question is: How can I test in this scenario.我的问题是:我如何在这种情况下进行测试。 I'm using Powermock for Android Testing.我正在使用Powermock进行 Android 测试。

Thanks :)谢谢 :)

If as you said the c() doesn't depends on state of a() and b() then you can make extract method object refactoring and test it separately.如果如您所说, c()不依赖于a()b()状态,那么您可以对提取方法对象进行重构并单独对其进行测试。 In case if you use factory then you won't need PowerMock.如果您使用工厂,那么您将不需要 PowerMock。

If a() and b() doesn't depends on c() result then the suppress method can be used.如果a()b()不依赖于c()结果,则可以使用抑制方法

If a() and b() depends on c() result, then you can create partially mock (by using spy) and mock the c() .如果a()b()取决于c()结果,那么您可以创建部分模拟(通过使用 spy)并模拟c()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 测试用例(场景)-一种或多种方法? - Test use case (scenario) - one or multiple methods? 在一种方法中调用太多的Firebase函数 - Calling too many firebase function in one method 我是否需要添加许多方法,或者可以调用一个方法 - Do I need to add many methods, or is it possible to call one method 如何防止一个方法在测试中调用另一个方法? - How to prevent a method from calling another method in test? 在其中调用预构建库的静态方法时,如何声明本机方法? - How to declare native methods when calling a static method of a prebuilt library in it? 有没有一种方法可以使用多种测试方法(仅一种设置方法)来运行Espresso测试? - Is there a way to run Espresso test with multiple test methods but only one setup method? 更好的方法-所有按钮都可以使用long onClick方法,或者每个按钮可以使用许多简短方法[android] - What is better - one long onClick method for all buttons, or many short methods for each button[android] Dagger2 + Mockito:如何对这种情况进行单元测试? - Dagger2 + Mockito: How to Unit Test this scenario? 接口的java类型作为方法参数-如何将两个方法组合为一个 - java type of interface as method parameter - how to combine two methods into one 在android的oncreate方法中调用多个方法 - calling multiple methods in oncreate method in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM