0

I have generated test cases that I need to execute. I have written my methods of the tests independent of each other and writing scripts to execute these methods via the adb is not helping since they dont execute in order of the sequence given it.

I would like to know a preferred approach to take ? Or how I can automate these multiple tests via the adb.

I have realized all most out there tend to use the adb commands under their codes so dont know if there is a tool that may be of help. I am open to that as well

Thank you

user3701188
  • 638
  • 3
  • 7
  • 23
  • https://developer.android.com/studio/test/command-line.html – ashkhn Apr 21 '17 at 01:43
  • @akash93 I am writing scripts already but the problem is my test cases are dependent on each other which is not encouraged. I want a way now write all my test cases and then execute them in the path I give it. E.g click button1 and then click button 2. But I dont know this before hand. So I want to execute the path at runtime. I hope this explanation is good enough – user3701188 Apr 21 '17 at 02:00

1 Answers1

0

I think there's a problem with your test case structure. Test cases should be independent of each other and that is not something that is only encouraged but often a necessity to ensure integrity of your tests.

If you have dependent actions then they need to be bundled in the same test case or you need to look at dependency injection/mocks.

That being said, JUnit4 has a @FixMethodOrder annotation which you can add to run the tests in order.

import org.junit.runners.MethodSorters;

import org.junit.FixMethodOrder;
import org.junit.Test;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class SampleTest {

   @Test
   public void firstTest() {
      System.out.println("first");
   }

   @Test
   public void secondTest() {
      System.out.println("second");
   }
}
ashkhn
  • 1,642
  • 1
  • 13
  • 18
  • i do understand that tests should be independent of each other. But I am trying to find a way to automatically execute an order with arranging them my self in the codes in android. I would prefer writing all my test actions in a single test method but these scenarios will be changing as I generate new test case order. So I would be glad if you have a suggestion for me. Another thing is I read fixmethodorder doesnt guarantee that one test finishes before the other – user3701188 Apr 21 '17 at 02:30
  • In that case the only thing I can think of is to convert your current tests into private methods and then create tests which call these private methods in the order you need – ashkhn Apr 21 '17 at 02:43
  • Can you share the resource where you read that FixMethodOrder doesn't guarantee order. As far as I'm aware it does since that's entire point of having that annotation – ashkhn Apr 21 '17 at 02:45
  • i will try find the resource and send it. But it didnt deny that fact that FixmethodOrder executes in an other. But it said you are not guaranteed of one finishing before the other. – user3701188 Apr 21 '17 at 09:55
  • Also I thought of your method you gave and that is putting them in private methods and executing these private methods. But the question I would like to ask is how do I do this from an external program like command line , because I have all my methods defined in my android but the paths that will be generated from my Models will determine which methods to execute. But I need to execute these methods in my test method. But the question is how. Do you have any ideas , I would be glad to know. Thank you. – user3701188 Apr 21 '17 at 09:58
  • Not sure I understand the problem but the link I shared in my first comment has instructions to get started on command line testing and more specifically the usage of the `am` command. You can use the `setup` method to mock your models and generate a specific order of paths and verify if the methods execute as per your requirement – ashkhn Apr 21 '17 at 18:56
  • hello @akash93 what do you mean by "You can use the setup method to mock your models and generate a specific order of paths and verify if the methods execute as per your requirement" if I may ask – user3701188 Apr 23 '17 at 21:25
  • junit offers `setUp` and `tearDown` methods that are run before every test executes. You can override these methods to initialize your models appropriately – ashkhn Apr 24 '17 at 02:05
  • I would I would try explain one more time my problem so make it more clear. I am doing a model-based testing project for android. I have generated test cases. Eg. clickbuttonA,clickButtonB,clickButtonC and it should be in this order. The question is how to automate this. I wrote scripts via the adb but they dont execute according to the order I give them. Do you have an idea or some suggestions if you get my point – user3701188 Apr 24 '17 at 14:07
  • You are using espresso right? It seems like you're manually trying to do things which are already provided by the framework – ashkhn Apr 25 '17 at 02:28
  • That is correct, I am using expresso. All these methods that is describing it is true that espresso that them. But look at it this way that I am generating test cases from a model. Example , after generation , I get these test cases. clickButton1() ,clickButton2(). These are already defined in my espresso but now I want to automate this process. So i want to be able to call clickButton1() and clickButto2() from my adb shell to automate this process – user3701188 Apr 25 '17 at 12:54