0

I have configured my Jenkins/Maven project, it reads my TestNG test from POM file configuration where is test.xml file with instructions which tests to run.

If I will add more tests to XML file (for example Test1 and Test2), they will be dependent on the same POM file, and when I create a job in Jenkins, both tests will run in one Jenkins job.

My question is:
How to configure Maven or Jenkins to be able to create two separate Jobs in Jenkins (Job Test1 and Job Test2) but using the single POM file?

catch23
  • 17,519
  • 42
  • 144
  • 217
  • Why do you need to run different sets of Tests? why not running all the tests all the time? Sounds like those tests do not belong to that module? – khmarbaise Apr 24 '17 at 16:19
  • Because my tests have DataProviders and each test run in a loop more than one time. So I want to see in Jenkins execution of each test separately as a separate task. – Postavshik Apr 24 '17 at 21:42

2 Answers2

2

My suggestion would be - create different Maven's profiles and execute it for specific Jenkin's job.

For example, you have 2 different test classes, you can create a specific profile for each at pom file:

<profiles>
    <profile>
        <id>test1</id>
        <properties>
            <suite.pattern>**/TestOne.java</suite.pattern>
        </properties>
    </profile>
    <profile>
        <id>test2</id>
        <properties>
            <suite.pattern>**/TestTwo.java</suite.pattern>
        </properties>
    </profile>

now you can run them separately:

mvn clean test -P test1

or

mvn clean test -P test2

Thus, you can configure two Jenkins jobs.

catch23
  • 17,519
  • 42
  • 144
  • 217
  • Awesome, I think this is what I need, but it s not clear how correctly to put it in POM file because right now I invoke testng.xml file which run my TestNG test. This file has a description which test to run. Can I get rid of this testng.xml and just run my tests invoking them with in POM file? – Postavshik Apr 24 '17 at 21:38
  • @Postavshik you can do this. Just comment you're plugin which calls `testng.xml` and add profiles section to pom. – catch23 Apr 25 '17 at 04:41
  • Thank you that work perfectly! I was googling about and found 0 info. I am surprised how did you know this method but it works. And one more question. Let's say I have two tests in one Java class and I want to run only one of them. What node whould I use and how? Thanks a lot! – Postavshik Apr 26 '17 at 03:24
  • @Postavshik you can use TestNG annotation for specific method `@Test(enabled = false)`. Also, if you need more control for some profile you can add a plugin to it -> and include more configuration for it. – catch23 Apr 26 '17 at 10:47
  • I would love to, but since I registered here recently and has low reputation, system not allow me to do that :( – Postavshik Apr 26 '17 at 23:46
  • I have a problem. When I try to run test2 it runs test1 anyway. But it s happened when I used [clean test -P test2] I thought mistake was because I did not use [mvn], when I added command how you suggested [mvn clean test -P test2] I have error in console [[ERROR] Unknown lifecycle phase "mvn". You must specify a valid lifecycle phase or a goal in the format : or :[:]:.] – Postavshik Apr 27 '17 at 13:28
  • @Postavshik try to use a different name for profile `test1` or `test2` is a just example. You have to check profile execution locally on your pc. And only when it works for you -> move configuration to CI – catch23 Apr 27 '17 at 14:17
  • Something crazy happening... I have deleted test1 with TestOne.java from my POM file at all, but when I run test, it s still executing TestOne.java. Only test2 with TestTwo.java left in POM file, but when I run the test, it invokes TestOne.java. What the heck? There is no single word about TestOne.java in my POM, why it runs this class? Please help :( – Postavshik Apr 27 '17 at 15:10
  • @Postavshik you have to read more about Maven lifecycle. If you are run `mvn test` it will execute all tests for your application (at your example both tests will be executed). If you want to execute some specific `profile` you need to write it down `mvn test -P `. please, have a look to this answer [How can I skip tests in maven install goal, while running them in maven test goal?](http://stackoverflow.com/questions/17117589/how-can-i-skip-tests-in-maven-install-goal-while-running-them-in-maven-test-goa) – catch23 Apr 27 '17 at 15:20
  • This does not work for me 1. when i run `mvn clean test -P ` I have ERROR in consol: [ERROR] Unknown lifecycle phase "mvn". You must specify a valid lifecycle phase or a goal in the format : or :[:]: 2. when i run `clean test -P ` with some reason it run class which is even NOT mentioned in the profile or in POM file at all. I change name of profile, change class name. Does not matter. It always run one the same class. I don't understand why :(( – Postavshik Apr 27 '17 at 15:35
  • @Postavshik Does it works locally on your machine? As can't remind if `mvn` is required for Jenkins. If locally everything goes fine. It is Jenkins issue -> and you have to fix configuration for CI. If locally you have `fail` -> you need to work on your project locally and make it works first. – catch23 Apr 27 '17 at 15:50
  • I have found an easier solution. You just need to run the command: `-Dtest=TestClass.java clean test` And it's not necessary to create profiles in POM file – Postavshik Apr 27 '17 at 16:56
0

I have found another solution. In Jenkins Project configuration need to write the command like this: -Dtest=FirefoxTest2.java clean test

Do not need to add any additional information about java classes in POM file.