Gradle Review (3)-use testng in Gradle projects

Source: Internet
Author: User
Tags testng
1.gradle Project


Java projects created in Eclipse are managed with Gradle. What I'm going to review is the knowledge of using testng in Gradle. First testng the environment configuration as in the previous article.

There are 2 Java files in the test directory. One is Persontest.java, one is simpletest.

Persontest.java: A test class written with JUnit.

Simpletest:testng write the test class.


public class Persontest {
    @Test public
    void Canconstructapersonwithaname () {person person
        = new Person (" Larry ");
        Assertequals ("Larry", Person.getname ());
    }
}


public class SimpleTest {
	@BeforeClass public
	void SetUp () {
		System.out.println ("Setup");
	}

	@Test (groups = {"Slow"}) public
	void Aslowtest () {
		System.out.println ("Slow Test");
	}

	@Test (groups = {"Fast"}) public
	void Afasttest () {
		System.out.println ("Fast Test");
	}
}

Build.gradle original script is as follows:

Build.gradle


Apply plugin: ' java '
apply plugin: ' eclipse '

sourcecompatibility = 1.7
Version = ' 1.0 '
jar {
    manifest {
        attributes ' implementation-title ': ' Gradle Quickstart ', ' implementation-version ': Version
    }
}

repositories {
    mavencentral ()
}

dependencies {
    compile group: ' Commons-collections ', Name: ' Commons-collections ', version: ' 3.2 '
    compile ' org.testng:testng:6.8.17 '
    testcompile group: ' JUnit ', Name: ' JUnit ', version: ' 4.+ '
}

task Copyjars (type:copy) {from
	configurations.runtime into
	' Libs '
}
uploadarchives {
    repositories {
       Flatdir {
           dirs ' repos '}}
}

Execute the command at this time and you will find that the JUnit case is executed by default.


D:\eclipse\workspare\testng_gradl>gradle build
: Compilejava
:p rocessresources
: Classes
: Jar
: Assemble
: Compiletestjava
:p rocesstestresources
: testclasses
: Test:
Check
: Build

Build Successful total

time:5.179 secs

The report reads as follows:




So how do I choose testng?


2. Perform testng test


Very simple, add the following statement in Build.gradle


Test {
usetestng ()
}

Perform a clean task before emptying the previous build file. Re-build:


D:\eclipse\workspare\testng_gradl>gradle clean
: Clean

BUILD Successful total

time:4.355 secs
D : \eclipse\workspare\testng_gradl>gradle build
: Compilejava
:p rocessresources
: Classes
: Jar
: Assemble
: Compiletestjava
:p rocesstestresources
: testclasses
: Test:
Check
: Build

Build Successful total

time:5.319 secs

You will find that the case of execution is testng case.



It's that simple.


3.TestNG Configuration


Report Configuration


By default, HTML files are present in the report, and we can choose not to generate HTML files.

Build.gradle:

Test {
	usetestng ()
	reports.html.enabled = False
}

This time the rebuild will no longer generate the reports file.

If we want to copy the report to a different path. Note that this is copy not redirection.


Task Testreport (type:testreport) {
	destinationdir = file ("d:/gradle_product/0120")
	Reporton test
}

This time clean is executing testreport:


D:\eclipse\workspare\testng_gradl>gradle clean testreport
: Clean
: Compilejava
:p rocessresources
: Classes
: Compiletestjava
:p rocesstestresources
: testclasses
: Test
: Testreport

BUILD Successful

Total time:5.447 secs

At this time you will find the relevant files in the corresponding directory.


Group Test


Select a group's case for testing

Build.gradle:


Test {
	usetestng{
		includegroups ' slow '
	}
	//reports.html.enabled = False
}

We chose to test the case for slow grouping. Re-build the View report as follows:




The discovery executes a case and points it in.



The grouping configuration was successful.


sub-class file testing


We tested it in terms of testng, and we tested it in a test class file, like we were testing a test item in a Java file. To differentiate, we have added a Simpletest1.java testng case class.


public class SimpleTest1 {
	@BeforeClass public
	void SetUp () {
		System.out.println ("Setup");
	}

	@Test (groups = {"Slow"}) public
	void ASlowTest1 () {
		System.out.println ("Slow test1");
	}

	@Test (groups = {"Fast"}) public
	void AFastTest1 () {
		System.out.println ("Fast test1");
	}
}

There are two ways to test SimpleTest1 at this time.

1.include contains the tested class file

Build.gradle:


Test {
usetestng ()
systemproperty ' Some.prop ', ' value '
include ' Org/gradle/simpletest1.class '
}




2. Since we have only 2 files in this case, we can test the effect of the SimpleTest1 class by culling the SimpleTest class.


The result of the test is the same as above, and no more stickers.


two combination of filtration conditions


Now let's use the grouping conditions and the sub-class file together to see the effect.

Above we only tested all the methods in Simpletest1.class, this time we use the grouping to filter, only test the simpletest1.class in the slow grouping method.


Build.gradle:


Test {
	usetestng{
		includegroups ' slow '
	}
	systemproperty ' Some.prop ', ' value '
	exclude ' org/ Gradle/simpletest.class '
	
}

This time we re-build and after execution you will find that only the ASlowTest1 method is tested.


Of course, filter groups can also be excludegroups ' fast ' and the above effect is the same, no longer an example.


The next thing to try is what happens when the two conditions are grouped together without finding the case to test.


Just now we tested the Simpletest1.class in the slow packet case, the add to Slow1, because Slow1 is not, so should not find the corresponding case in Simpletest1.class, see this build result is what kind of.

Build.gradle


Test {
	usetestng{
		includegroups ' Slow1 '
	}
	systemproperty ' Some.prop ', ' value '
	exclude ' org/ Gradle/simpletest.class '
	
}

The result file is as follows, and the resulting file is available, but you can see that the result file does not have information about the case, stating that no case was executed.





3. Configure with Testng.xml


You can also use Testng.xml to configure a test case in a Gradle project


We start by creating a new Testng.xml file:


<?xml version= "1.0" encoding= "UTF-8"?> <suite name= "doctorng" > <!--enabled= "true" for the test to take effect, Some tests can be turned on or off as appropriate-<test name= "test" enabled= "true" > <!--Specify Parameters--<parameter name= "acc
            Esskey "value=" f0af8e412cef7e5058beeb6df2012e1e "/> <!--Specify test package, note Add. *--<packages>
            <package name= "org.gradle.*"/> </packages> <!--specifying test class--<classes> <class name= "Org.gradle.SimpleTest"/> <!--Filtering test class Method--<class name= "Org.grad Le. SimpleTest1 "> <methods> <include name=" Afasttest "/> </metho Ds> </class> </classes> <!--assigning test groupings--<groups> &L T;run> <!--included-<include name= "Fast"/> <!--excluded--&lt ; Exclude name= "Slow"/> </run> </groups> </test> </suite> 

Then get the contents of the file in Build.gradle:


Test {
	usetestng{
			Suites (file (' Src/test/resources/org/gradle/testng.xml '));
	}
	Beforetest {descriptor-
      logger.lifecycle ("Running test:" + descriptor)
   }
   onoutput {descriptor, Event
      logger.lifecycle ("Test:" + descriptor + "produced standard Out/err:" + event.message)
   }
}

The focus is on the closure code in usetestng, which puts Testng.xml in.


D:\eclipse\workspare\testng_gradl>gradle clean Test:clean:compileJava:p rocessresources:classes:
Compiletestjava:p rocessTestResources:testClasses:test Running test:test method Afasttest (org.gradle.SimpleTest) Test:test method Afasttest (org.gradle.SimpleTest) produced standard OUT/ERR:FA St Test Running Test:test method Afastt Est1 (org.gradle.SimpleTest) Test:test method AFastTest1 (org.gradle.SimpleTest) produced standard out/err:f ast Test1 Ru Nning Test:test Method AFastTest2 (Org.gradle.SimpleTest) Test:test method AFastTest2 (Org.gradle.SimpleTest) produced Standard out/err:f ast test2 Running Test:test method AFastTest3 (Org.gradle.SimpleTest) Test:test method AFastTest3 (or
g.gradle.simpletest) produced standard out/err:f ast test3 Running Test:test method AFastTest1 (Org.gradle.SimpleTest1)  Test:test method AFastTest1 (ORG.GRADLE.SIMPLETEST1) produced standard out/err:fast test1 BUILD successful total time: 5.741 secs d:\eclipse\workspare\testng_gradl>


The test report is as follows:





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.