TestNG : Test case is ignored

我们两清 提交于 2020-01-05 04:37:33

问题


I am using TestNG framework for writing test cases for my Android application. For which I am using Appium testing tool.

For this I have defined following files :

  • pom.xml file - required for dependencies
  • One BaseTest.java class
  • Two child classes which is extended from BaseTest.java
  • testng.xml file - defines running test classes in it.

For better understanding of my question posting classes & xml files.

This is pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example.testing</groupId>
    <artifactId>android-appium</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.14.3</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.appium</groupId>
            <artifactId>java-client</artifactId>
            <version>7.1.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

This is BaseTest.java class

import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;

public class BaseTest {


    @BeforeSuite
    public void setUp()
    {
    }

    @AfterSuite
    public void tearDown()
    {

    }
}

This is FirstTest.java class

import io.appium.java_client.MobileBy;
import io.appium.java_client.MobileElement;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.AndroidMobileCapabilityType;
import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.touch.WaitOptions;
import io.appium.java_client.touch.offset.PointOption;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import scenarios.BaseTest;

import java.net.MalformedURLException;
import java.net.URL;
import java.text.DateFormat;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class FirstTest extends BaseTest {

    private AndroidDriver<MobileElement> mAndroidDriver;

    @BeforeTest
    protected  void setUpDriver() throws MalformedURLException {

        DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
        desiredCapabilities.setCapability("device", "Android");

        desiredCapabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "abfg34e");
        desiredCapabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");

        desiredCapabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "7.0");
        desiredCapabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UiAutomator1");
        desiredCapabilities.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, "com.example.test");

        desiredCapabilities.setCapability(MobileCapabilityType.APP,"/home/desktop/app-developer-debug.apk");
        desiredCapabilities.setCapability(MobileCapabilityType.NO_RESET, "true");

        mAndroidDriver = new AndroidDriver(new URL(Constants.BASE_URL), desiredCapabilities);
        System.out.println("setUpDriver() :: time : "+ DateFormat.getDateTimeInstance().format(System.currentTimeMillis()));
    }

    @Test(groups = "app_screen_group_1", priority = 1)
    public void splashScreen_1() throws InterruptedException {
        System.out.println("splashScreen_1() :: startTime : "+ DateFormat.getDateTimeInstance().format(System.currentTimeMillis()));
        Thread.sleep(7000);
    }


    @Test(groups = "app_screen_group_1", priority = 2)
    public void splashScreen_2() throws InterruptedException {
        System.out.println("splashScreen_2() :: startTime : "+ DateFormat.getDateTimeInstance().format(System.currentTimeMillis()));
        MobileElement menuElement = mAndroidDriver.findElementByAccessibilityId("More options");
        menuElement.click();

        Thread.sleep(10);
        MobileElement splashElement = mAndroidDriver.findElementByAndroidUIAutomator("new UiSelector().text(\"Splash\")");
        splashElement.click();
    }
}

This is SecondTest.java class

 import io.appium.java_client.MobileBy;
    import io.appium.java_client.MobileElement;
    import io.appium.java_client.TouchAction;
    import io.appium.java_client.android.AndroidDriver;
    import io.appium.java_client.remote.AndroidMobileCapabilityType;
    import io.appium.java_client.remote.MobileCapabilityType;
    import io.appium.java_client.touch.WaitOptions;
    import io.appium.java_client.touch.offset.PointOption;
    import org.openqa.selenium.remote.DesiredCapabilities;
    import org.testng.annotations.BeforeTest;
    import org.testng.annotations.Test;
    import scenarios.BaseTest;

    import java.net.MalformedURLException;
    import java.net.URL;
    import java.text.DateFormat;
    import java.time.Duration;
    import java.util.List;
    import java.util.concurrent.TimeUnit;

    public class SecondTest extends BaseTest {

        private AndroidDriver<MobileElement> mAndroidDriver;


        @Test(groups = "app_screen_group_2", priority = 1)
        public void logInScreen_1() throws InterruptedException {
            System.out.println("logInScreen_1() :: startTime : "+ DateFormat.getDateTimeInstance().format(System.currentTimeMillis()));
            Thread.sleep(7000);
        }


        @Test(groups = "app_screen_group_2", priority = 2)
        public void logInScreen_2() throws InterruptedException {
            System.out.println("logInScreen_2() :: startTime : "+ DateFormat.getDateTimeInstance().format(System.currentTimeMillis()));
            MobileElement menuElement = mAndroidDriver.findElementByAccessibilityId("More options");
            menuElement.click();

            Thread.sleep(10);
            MobileElement logInElement = mAndroidDriver.findElementByAndroidUIAutomator("new UiSelector().text(\"Log in\")");
            logInElement.click();
        }
    } 

This is testng.xml file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="androidapp" group-by-instances="true">

    <test name="FirstScenario_1" >

        <classes>
            <class name="scenarios.FirstTest" ></class>
            <class name="scenarios.SecondTest"></class>
        </classes>
    </test>

   <!-- Following scenario runs perfectly if I have each separate class in separate test name. But in above case scenario it is not working properly, it gives Test ignored error for second method of FirstTest.java
<test name="secondScenario_1" >

        <classes>
            <class name="scenarios.FirstTest" ></class>
        </classes>
    </test>

 <test name="secondScenario_2" >

        <classes>
            <class name="scenarios.SecondTest" ></class>
        </classes>
    </test>-->
</suite>

When I run this code using appium tool then on second function splashScreen_2() of FirstTest.java class got error Test ignored & it is not running properly. But when I do uncomment secondScnario_2 in testng.xml file & comment FirstScenario_1 then my test cases run properly (as I mention in comment also) & android app executes each function properly one by one.

But I want to do execute all classes in <test> </test> functions in testng.xml.

If I use secondScnario_2 in testng.xml file then I need to give separate test name for each scenario. And I want to use only one test name. So here when I use FirstScenario_1 in testng.xml file, Why is their an error of test ignored ocurring here?


回答1:


Based on what you have described in comments , I think you want to run everything in order by defining all classes in one test. Then you should remove priorities and groups and run it with this xml with preserve-order="true" . This should run test in the order they are defined in xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="androidapp" >

    <test name="FirstScenario_1" preserve-order="true">

        <classes>  
         <class name="scenarios.FirstTest" >
               <methods> 
                  <include name="setUpDriver" />
                  <include name="splashScreen_1" /> 
                  <include name="splashScreen_2" />
               </methods> 
        </class>
            <class name="scenarios.SecondTest">
               <methods> 
                  <include name="logInScreen_1" />
                  <include name="logInScreen_2" />
               </methods>
          </class>
        </classes>
    </test>
</suite>

You can also use @dependsOnMethods to run methods in the order you want . Have a look at this . The ordering described there should also help you to resolve this



来源:https://stackoverflow.com/questions/58027931/testng-test-case-is-ignored

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!