[英]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.