Testng Framework published real machine test on AndroidStudio

Testng Framework published real machine test on AndroidStudio

Link to the code in this article:https://blog.csdn.net/weixin_39451323/article/details/114030393

1.1 Index Structure

For use cases on Android phones, you need to put the test code in the AndroidTest folder (the unit test is under the test folder), otherwise it can’t run.

  • Catalog structure

1.2 Gradle download

1.3 Configure Gradle

  • gradle-wrapper.properties

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.3-all.zip
plugins {
   id 'com.android.application'
}

android {
   compileSdkVersion 30
   buildToolsVersion "30.0.3"

   defaultConfig {
       applicationId "yuyang.example.hellowng"
       minSdkVersion 16
       targetSdkVersion 30
       versionCode 1
       versionName "1.0"

//        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
       testInstrumentationRunner 'de.lemona.android.testng.TestNGRunner'
   }

   buildTypes {
       release {
           minifyEnabled false
           proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
       }
   }
   compileOptions {
       sourceCompatibility JavaVersion.VERSION_1_8
       targetCompatibility JavaVersion.VERSION_1_8
   }
}

dependencies {

   implementation 'androidx.appcompat:appcompat:1.2.0'
   implementation 'com.google.android.material:material:1.3.0'
   implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
   testImplementation 'junit:junit:4.+'
   testImplementation 'org.testng:testng:6.9.6'
//    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
   implementation 'de.lemona.android:android-testng:1.1.27'
   androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()
        jcenter()
        maven {
            url 'http://dl.bintray.com/lemonade/maven'
        }
    }
    dependencies {
        classpath "com.android.tools.build:gradle:3.3.3"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven {
            url 'http://dl.bintray.com/lemonade/maven'
        }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

Noticeable: build.gradle(:app)Center'de.lemona.android:android-testng:1.1.27'.There is only one link on the actors page,there are multiple casts) and it’s not available on google() or jCenter() so you have to be in **build.gradle(:heellowNG)Add title'http://dl.bintray.com/lemonade/maven'
Testng link

Like themaven-metadata.xmldisplay, the latest version is 1.1.27

<?xml version="1.0" encoding="UTF-8"?>
<metadata>
  <groupId>de.lemona.android</groupId>
  <artifactId>android-testng</artifactId>
  <version>1.1.27</version>
  <versioning>
    <latest>1.1.27</latest>
    <release>1.1.27</release>
    <versions>
      <version>1.0.2</version>
      <version>1.0.5</version>
      <version>1.0.20</version>
      <version>1.0.21</version>
      <version>1.1.22</version>
      <version>1.1.23</version>
      <version>1.1.25</version>
      <version>1.1.26</version>
      <version>1.1.27</version>
    </versions>
    <lastUpdated>20160803102016</lastUpdated>
  </versioning>
</metadata>
  • Ordertestng.xml
<?xml version="1.0"  encoding="utf-8"?>

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="testNG Suite" >
    <test name="ThreadTest3">
        <classes name="3">
            <class name="yuyang.example.hellowng.SuiteTest1"></class>
        </classes>
    </test>
</suite>

Enter AndroidStudio terminal entrygradlew cAT(Representing the use of Gradle
w’rapper to connect android test)

SuiteTest1.java

package yuyang.example.hellowng;
import java.lang.reflect.Method;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class SuiteTest1 {

   @Test(dataProvider = "dataInfoMethod", invocationCount = 10,threadPoolSize = 4)
   public void userInfo1(String name, String id){
       System.out.println(name+"Вход выполнен успешно!" + "серийный номер:"+id  +"  Current Thread id: "+Thread.currentThread().getId());
   }

   @Test(dataProvider = "dataInfoMethod")
   public void userInfo2(String name, String id){
       System.out.println(name+"Вход выполнен успешно!" + "серийный номер:"+id );
   }

   @DataProvider(name = "dataInfoMethod")
   public Object[][] dataProviderMethod(Method method){
       if (method.getName().equals("userInfo1")){
           return new Object[][]{{"Allen", "001"}};
       }
       else if (method.getName().equals("userInfo2")){
           return new Object[][]{{"Mary", "002"}};
       }
       return null;
   }
}



Work result:

You can view L to OG while running the real machine process:

Success here! Start the journey of automated testing, unit testing, and test development!

Leave a Comment