-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathbeanshell.html
100 lines (100 loc) · 4.08 KB
/
beanshell.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="Asciidoctor 2.0.18">
<title>BeanShell and advanced group selection</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:300,300italic,400,400italic,600,600italic%7CNoto+Serif:400,400italic,700,700italic%7CDroid+Sans+Mono:400,700">
<link rel="stylesheet" href="./asciidoctor.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prettify/r298/prettify.min.css">
</head>
<body class="article toc2 toc-left">
<div id="header">
<div id="toc" class="toc2">
<div id="toctitle">Table of Contents</div>
<ul class="sectlevel2">
<li><a href="#_beanshell_and_advanced_group_selection">BeanShell and advanced group selection</a></li>
</ul>
</div>
</div>
<div id="content">
<div class="sect2">
<h3 id="_beanshell_and_advanced_group_selection">BeanShell and advanced group selection</h3>
<div class="paragraph">
<p>If the <code><include></code> and <code><exclude></code> tags in <code>testng.xml</code> are not enough for your needs, you can use a <a href="https://beanshell.org/">BeanShell</a> expression to decide whether a certain test method should be included in a test run or not. You specify this expression just under the <code><test></code> tag:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="xml"><test name="BeanShell test">
<method-selectors>
<method-selector>
<script language="beanshell">
<![CDATA[
groups.containsKey("test1")
]]>
</script>
</method-selector>
</method-selectors>
<!-- ... -->
</test></code></pre>
</div>
</div>
<div class="paragraph">
<p>When a <code><script></code> tag is found in testng.xml, TestNG will ignore subsequent <code><include></code> and <code><exclude></code> of groups and methods in the current <code><test></code> tag: your BeanShell expression will be the only way to decide whether a test method is included or not.</p>
</div>
<div class="paragraph">
<p>Here are additional information on the BeanShell script:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>It must return a boolean value. Except for this constraint, any valid BeanShell code is allowed (for example, you might want to return <code>true</code> during week days and <code>false</code> during weekends, which would allow you to run tests differently depending on the date).</p>
</li>
<li>
<p>TestNG defines the following variables for your convenience:</p>
<div class="ulist">
<ul>
<li>
<p><code>java.lang.reflect.Method method</code>: the current test method.</p>
</li>
<li>
<p><code>org.testng.ITestNGMethod testngMethod</code>: the description of the current test method.</p>
</li>
<li>
<p><code>java.util.Map<String, String> groups</code>: a map of the groups the current test method belongs to.</p>
</li>
</ul>
</div>
</li>
<li>
<p>You might want to surround your expression with a CDATA declaration (as shown above) to avoid tedious quoting of reserved XML characters).</p>
</li>
</ul>
</div>
<div class="admonitionblock tip">
<table>
<tr>
<td class="icon">
<i class="fa icon-tip" title="Tip"></i>
</td>
<td class="content">
Starting from version <code>7.5</code> TestNG does not bring in any dependency on BeanShell implementations by default.
So in order to leverage BeanShell based method selectors, please remember to add an explicit dependency on BeanShell.
For example <a href="https://mvnrepository.com/artifact/org.apache-extras.beanshell/bsh">org.apache-extras.beanshell</a>
</td>
</tr>
</table>
</div>
</div>
</div>
<div id="footer">
<div id="footer-text">
Last updated 2025-02-27 18:51:48 UTC
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prettify/r298/run_prettify.min.js"></script>
</body>
</html>