Issue
I'm new to the automated testing, and using espresso to test my android App.
The problem is that I have multiple dynamic views depending on certain conditions :
My user has a boolean attribute, let's call it "isPremium"
when I click on a button my user is redirected to FragmentA if isPremuim == true , else he's redirected to FragmentB.
now for my tests I have
@Test public void testFragmentA();
and
@Test public void testFragmentB();
but when I run my tests based on my data, forcibly one of the two tests fails.
so should i make one test for both fragments like
private void testFragmentA();
private void testFragmentB();
@Test
public void myGlobalTest {
if(user.isPremium) testFragmentA();
else testFragmentB();
}
is this the right way to make my tests ? or there is another better way, because sincerly I'm not convinced with this method.
Solution
It would be best if you set value for premium at the beginning of each test (true for testFragmentA, false for testFragmentB). That way you will know what you are expecting and what each fragment depends on. Also, if user is some global variable, you should keep its state in @Before and restore it in @After method.
boolean isPremium;
@Before
public void init() {
isPremium = User.isPremium();
}
@Test
public void testFragmentA(){
User.setPremium(true);
// test fragment A
}
@Test
public void testFragmentB(){
User.setPremium(false);
// test fragment B
}
@After
public void restore() {
User.setPremium(isPremium);
}
Answered By - Crepi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.