Execution of tests in testng

I have 2 tests that I need to run one after another for several times.

The desired scenario is: “first test”, “second test”, “first test”, “second test” and so on…

The actual scenario is: “first test”, “first test”, “second test”, “second test”.

@Test (priority = 1, invocationCount = 3)
public void first() {
System.out.println(“first test”);
}

@Test (priority = 2, invocationCount = 3)
public void second() {
System.out.println(“second test”);
}
How can I achieve my desired scenario?

Another requirement here is that on the first test, android phone should be the first device and ios phone should be the second device. On the second test, the ios phone should be the first device and the android should be second. So it means that I need to use a different xml file.

    <test name="TwoDevices - ios first">
<parameter name="appName" value="App2"/>
<parameter name ="device" value="IOS/iphone6_plus"/>
<parameter name ="secondDevice" value="ANDROID/lg4_v5"/>
<classes>
    <class name="com.TestFactory"/>
</classes>

example with 2 runs (third just add in same way):

<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" >
<suite name="my_suite">
        <parameter name="appName" value="App2"/>
        <test name="TwoDevices - ios first">
                <parameter name ="device" value="IOS/iphone6_plus"/>
                <parameter name ="secondDevice" value="ANDROID/lg4_v5"/>
                <classes>
                    <class name="com.TestFactory">
                        <methods>
                            <include name="first"></include>
                            <include name="second"></include>
                        </methods>
                    </class>
                </classes>
        </test>
        <test name="TwoDevices - ios second">
                <parameter name ="device" value="ANDROID/lg4_v5"/>
                <parameter name ="secondDevice" value="IOS/iphone6_plus"/>
                <classes>
                    <class name="com.TestFactory">
                        <methods>
                            <include name="first"></include>
                            <include name="second"></include>
                        </methods>
                    </class>
                </classes>
        </test>
</suite>

thanks for your reply.
i tried your suggestion with a minor change and it is working:

<test name="TwoDevices - ios first">
    <parameter name="appName" value="App2"/>
    <parameter name ="device" value="IOS/iphone6_plus"/>
    <parameter name ="secondDevice" value="ANDROID/lg4_v5"/>
    <classes>
        <class name="com.MyDebug">
        <methods>
            <include name="first"></include>
        </methods>
        </class>
    </classes>
</test>

<test name="TwoDevices - ANDROID first">
    <parameter name="appName" value="App2"/>
    <parameter name="device" value="ANDROID/lg4_v5"/>
    <parameter name="secondDevice" value="IOS/iphone6_plus"/>
    <classes>
    <class name="com.MyDebug">
        <methods>
            <include name="second"></include>
        </methods>
    </class>
    </classes>
</test>

My question is if in order to run the tests non stop one after another then I need to add 40 more times for example the lines to the xml file or i can use an annotation or loop or any other thing?

thnaks

when one test fail e.g. 4th. what is expected behaviour? do we continue regardless?

when one test fails then we don’t continue. i need the tests to run successfully each time i execute them.

any solution for this?

thanks

suggest to look for testNG+dataprovider. example: https://examples.javacodegeeks.com/enterprise-java/testng/testng-dataprovider-example/