/
TestNGWrapperSuite.scala
137 lines (123 loc) · 5.3 KB
/
TestNGWrapperSuite.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* Copyright 2001-2013 Artima, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.scalatestplus.testng
import org.scalatest._
import org.testng.TestNG
import org.testng.TestListenerAdapter
/**
* <p>
* Suite that wraps existing TestNG test suites, described by TestNG XML config files. This class allows
* existing TestNG tests written in Java to be run by ScalaTest.
* </p>
*
* <p>
* One way to use this class is to extend it and provide a list of one or more
* names of TestNG XML config file names to run. Here's an example:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">class</span> <span class="stType">MyWrapperSuite</span> <span class="stReserved">extends</span> <span class="stType">TestNGWrapperSuite</span>(
* <span class="stType">List</span>(<span class="stQuotedString">"oneTest.xml"</span>, <span class="stQuotedString">"twoTest.xml"</span>, <span class="stQuotedString">"redTest.xml"</span>, <span class="stQuotedString">"blueTest.xml"</span>)
* )
* </pre>
*
* <p>
* You can also specify TestNG XML config files on <code>Runner</code>'s command line with <code>-t</code> parameters. See
* the documentation for <code>Runner</code> for more information.
* </p>
*
* <p>
* To execute <code>TestNGWrapperSuite</code>s with ScalaTest's <code>Runner</code>, you must include TestNG's jar file on the class path or runpath.
* This version of <code>TestNGSuite</code> was tested with TestNG version 6.3.1.
* </p>
*
* @author Josh Cough
*/
class TestNGWrapperSuite(xmlSuiteFilenames: List[String]) extends TestNGSuite {
// Probably mention FileNotFoundException here
// If any files contained in the property cannot be found, a FileNotFoundException will be thrown.
/**
* Runs TestNG with the XML config file or files provided to the primary constructor, passing reports to the specified <code>Reporter</code>.
*
* @param testName If present (Some), then only the method with the supplied name is executed and groups will be ignored.
* @param args the <code>Args</code> for this run
*/
override def run(testName: Option[String], args: Args): Status = {
import args._
val tagsToInclude =
filter.tagsToInclude match {
case None => Set[String]()
case Some(tti) => tti
}
val tagsToExclude = filter.tagsToExclude
val status = new StatefulStatus
runTestNG(reporter, tagsToInclude, tagsToExclude, tracker, status)
status.setCompleted()
status
}
/**
* Runs all tests in the xml suites.
* @param reporter the reporter to be notified of test events (success, failure, etc)
*/
override private[testng] def runTestNG(reporter: Reporter, tracker: Tracker, status: StatefulStatus): Unit = {
runTestNG(reporter, Set[String](), Set[String](), tracker, status)
}
/**
* Executes the following:
*
* 1) Calls the super class to set up groups with the given groups Sets.
* 2) Adds the xml suites to TestNG
* 3) Runs TestNG
*
* @param reporter the reporter to be notified of test events (success, failure, etc)
* @param groupsToInclude contains the names of groups to run. only tests in these groups will be executed
* @param groupsToExclude tests in groups in this Set will not be executed
* @param status Run status.
*/
private[testng] def runTestNG(reporter: Reporter, groupsToInclude: Set[String],
groupsToExclude: Set[String], tracker: Tracker, status: StatefulStatus): Unit = {
val testng = new TestNG
handleGroups(groupsToInclude, groupsToExclude, testng)
addXmlSuitesToTestNG(testng)
run(testng, reporter, tracker, status)
}
// TODO: We should probably do this checking in the constructor.
/**
* TestNG allows users to programmatically tell it which xml suites to run via the setTestSuites method.
* This method takes a java.util.List containing java.io.File objects, where each file is a TestNG xml suite.
* TestNGWrapperSuite takes xmlSuitesPropertyName in its constructor. This property should contain
* the full paths of one or more xml suites, comma seperated. This method simply creates a java.util.List
* containing each xml suite contained in xmlSuitesPropertyName and calls the setTestSuites method on the
* given TestNG object.
*
* @param testng the TestNG object to set the suites on
*
* @throws FileNotFoundException if a file in xmlSuitesPropertyName does not exist.
*
*/
private def addXmlSuitesToTestNG(testng: TestNG): Unit = {
import java.io.File
import java.io.FileNotFoundException
val files = new java.util.ArrayList[String]
xmlSuiteFilenames.foreach( { name =>
val f = new File( name )
if( ! f.exists ) throw new FileNotFoundException( f.getAbsolutePath )
files add name
}
)
testng.setTestSuites(files)
}
}