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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764
|
<html>
<head>
<title>TestNG</title>
<link rel="stylesheet" href="testng.css" type="text/css" />
<link type="text/css" rel="stylesheet" href="http://beust.com/beust.css" />
<script type="text/javascript" src="banner.js"></script>
<script type="text/javascript" src="http://beust.com/scripts/shCore.js"></script>
<script type="text/javascript" src="http://beust.com/scripts/shBrushJava.js"></script>
<script type="text/javascript" src="http://beust.com/scripts/shBrushXml.js"></script>
<script type="text/javascript" src="http://beust.com/scripts/shBrushBash.js"></script>
<script type="text/javascript" src="http://beust.com/scripts/shBrushPlain.js"></script>
<link type="text/css" rel="stylesheet" href="http://beust.com/styles/shCore.css"/>
<link type="text/css" rel="stylesheet" href="http://beust.com/styles/shThemeCedric.css"/>
<script type="text/javascript">
SyntaxHighlighter.config.clipboardSwf = 'scripts/clipboard.swf';
SyntaxHighlighter.defaults['gutter'] = false;
SyntaxHighlighter.all();
</script>
<script type="text/javascript" src="http://beust.com/toc.js"></script>
<style type="text/css">
/* Set the command-line table option column width. */
#command-line colgroup.option {
width: 7em;
}
</style>
</head>
<body onLoad="generateToc();">
<script type="text/javascript">
displayMenu("documentation-main.html")
</script>
<h2 align="center">TestNG</h2>
<!-- --------------------------
<table class="float-right">
<tr>
<td>
<script type="text/javascript"><!--
google_ad_client = "pub-1467757024002850";
google_ad_width = 120;
google_ad_height = 600;
google_ad_format = "120x600_as";
google_ad_channel ="5560744946";
//-->
<!--
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</td>
</tr>
</table>
------------------------------- -->
<!-------------------------------------
TOC
------------------------------------>
<h3>Table of Contents</h3>
<div id="table-of-contents">
</div>
<!-------------------------------------
INTRODUCTION
------------------------------------>
<h3><a class="section" name="introduction">Introduction</a></h3>
TestNG is a testing framework designed to simplify a broad range of testing needs, from unit testing (testing a class in isolation of the others) to integration testing (testing entire systems made of several classes, several packages and even several external frameworks, such as application servers).
<p>
Writing a test is typically a three-step process:
<ul>
<li>Write the business logic of your test and insert <a href="#annotations">TestNG annotations</a> in your code.
</li>
<li>Add the information about your test (e.g. the class name, the groups you wish to run, etc...) in a <tt><a href="#testng-xml">testng.xml</a></tt> file or in build.xml.
</li>
<li><a href="ant.html">Run TestNG</a>.
</li>
</ul>
You can find a quick example on the <a href="index.html">Welcome page</a>.
<p>
The concepts used in this documentation are as follows:
<ul>
<li>
A suite is represented by one XML file. It can contain one or more tests and is defined by the <tt><suite></tt> tag.
</li>
<li>
A test is represented by <tt><test></tt> and can contain one or more TestNG classes.
</li>
<li>
A TestNG class is a Java class that contains at least one TestNG annotation. It is represented by the <tt><class></tt> tag and can contain one or more test methods.
</li>
<li>
A test method is a Java method annotated by <tt>@Test</tt> in your source.
</li></ul>A TestNG test can be configured by <tt>@BeforeXXX and @AfterXXX </tt>annotations which allows to perform some Java logic before and after a certain point, these points being either of the items listed above.<p>
The rest of this manual will explain the following:
<p>
<ul>
<li>A list of all the annotations with a brief explanation. This will give you an idea of the various functionalities offered by TestNG but you will probably want to consult the section dedicated to each of these annotations to learn the details.
</li>
<li>A description of the testng.xml file, its syntax and what you can specify in it.
</li>
<li>A detailed list of the various features and how to use them with a combination of annotations and testng.xml.
</li>
</ul>
<!-------------------------------------
ANNOTATIONS
------------------------------------>
<h3><a class="section" name="annotations">Annotations</a></h3>
Here is a quick overview of the annotations available in TestNG along with their attributes.
<p>
<table>
<tr>
<td colspan="2"><b><tt>@BeforeSuite<br>@AfterSuite<br>@BeforeTest<br>@AfterTest<br>@BeforeGroups<br>@AfterGroups<br>@BeforeClass<br>@AfterClass<br>@BeforeMethod<br>@AfterMethod</tt></b></td><td><b>Configuration information for a TestNG class:</b>
<br>
<br><b>@BeforeSuite: </b>The annotated method will be run before all tests in this suite have run.
<br><b>@AfterSuite: </b> The annotated method will be run after all tests in this suite have run.
<br><b>@BeforeTest</b>: The annotated method will be run before any test method belonging to the classes inside the <test> tag is run.
<br><b>@AfterTest</b>: The annotated method will be run after all the test methods belonging to the classes inside the <test> tag have run.
<br><b>@BeforeGroups</b>: The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked.
<br><b>@AfterGroups</b>: The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked.
<br><b>@BeforeClass</b>: The annotated method will be run before the first test method in the current class is invoked.
<br><b>@AfterClass</b>: The annotated method will be run after all the test methods in the current class have been run.
<br><b>@BeforeMethod</b>: The annotated method will be run before each test method.
<br><b>@AfterMethod</b>: The annotated method will be run after each test method.
</td>
</tr>
<tr>
<td>
</td>
<td>
<tt>alwaysRun</tt>
</td>
<td>
For before methods (beforeSuite, beforeTest, beforeTestClass and
beforeTestMethod, but not beforeGroups):
If set to true, this configuration method will be run
regardless of what groups it belongs to.
<br>
For after methods (afterSuite, afterClass, ...):
If set to true, this configuration method will be run
even if one or more methods invoked previously failed or
was skipped.
</td>
</tr>
<tr>
<td>
</td>
<td>
<tt>dependsOnGroups</tt>
</td>
<td>
The list of groups this method depends on.
</td>
</tr>
<tr>
<td>
</td>
<td>
<tt>dependsOnMethods</tt>
</td>
<td>
The list of methods this method depends on.
</td>
</tr>
<tr>
<td>
</td>
<td>
<tt>enabled</tt>
</td>
<td>
Whether methods on this class/method are enabled.
</td>
</tr>
<tr>
<td>
</td>
<td>
<tt>groups</tt>
</td>
<td>
The list of groups this class/method belongs to.
</td>
</tr>
<tr>
<td>
</td>
<td>
<tt>inheritGroups</tt>
</td>
<td>
If true, this method will belong to groups specified in the @Test annotation at the class level.
</td>
</tr>
<tr class="separator">
<td colspan="3"> </td>
</tr>
<tr>
<td colspan="2"><tt><b>@DataProvider</b></tt></td><td><b>Marks a method as supplying data for a test method. The annotated method must return an Object[][] where each Object[] can be assigned the parameter list of the test method. The @Test method that wants to receive data from this DataProvider needs to use a dataProvider name equals to the name of this annotation.</b></td></tr><tr>
<td>
</td>
<td>
<tt>name</tt>
</td>
<td>
The name of this data provider. If it's not supplied, the name of this data provider will automatically be set to the name of the method.
</td>
</tr>
<tr>
<td>
</td>
<td>
<tt>parallel</tt>
</td>
<td>
If set to true, tests generated using this data provider are run in parallel. Default value is false.
</td>
</tr>
<tr>
<td colspan="3"> </td>
</tr>
<tr>
<td colspan="2"><b><tt>@Factory</tt></b></td><td><b> Marks a method as a factory that returns objects that will be used by TestNG as Test classes. The method must return Object[].</b></td></tr><tr>
<td colspan="3"> </td>
</tr>
<tr>
<td colspan="2"><b><tt>@Listeners</tt></b></td><td><b>Defines listeners on a test class.</b></td></tr>
<tr>
<td></td>
<td>
<tt>value</tt>
</td>
<td>
An array of classes that extend <tt>org.testng.ITestNGListener</tt>.
</td>
</tr>
<tr>
<td colspan="3"> </td>
</tr>
<td colspan="2"><b><tt>@Parameters</tt></b></td><td><b>Describes how to pass parameters to a @Test method.</b></td></tr><tr>
<td>
</td>
<td>
<tt>value</tt>
</td>
<td>
The list of variables used to fill the parameters of this method.
</td>
</tr>
<tr>
<td colspan="3"> </td>
</tr>
<tr>
<td colspan="2"><b>@Test</b></td><td><b>Marks a class or a method as part of the test.</b></td></tr><tr>
<td>
</td>
<td>
<tt>alwaysRun</tt>
</td>
<td>
If set to true, this test method will always be run even if it depends on a method that failed.
</td>
</tr>
<tr>
<td>
</td>
<td>
<tt>dataProvider</tt>
</td>
<td>
The name of the data provider for this test method.
</td>
</tr>
<tr>
<td>
</td>
<td>
<tt>dataProviderClass</tt>
</td>
<td>
The class where to look for the data provider. If not specified, the data provider will be looked on the class of the current test method or one of its base classes. If this attribute is specified, the data provider method needs to be static on the specified class.
</td>
</tr>
<tr>
<td>
</td>
<td>
<tt>dependsOnGroups</tt>
</td>
<td>
The list of groups this method depends on.
</td>
</tr>
<tr>
<td>
</td>
<td>
<tt>dependsOnMethods</tt>
</td>
<td>
The list of methods this method depends on.
</td>
</tr>
<tr>
<td>
</td>
<td>
<tt>description</tt>
</td>
<td>
The description for this method.
</td>
</tr>
<tr>
<td>
</td>
<td>
<tt>enabled</tt>
</td>
<td>
Whether methods on this class/method are enabled.
</td>
</tr>
<tr>
<td>
</td>
<td>
<tt>expectedExceptions</tt>
</td>
<td>
The list of exceptions that a test method is expected to throw. If no exception or a different than one on this list is thrown, this test will be marked a failure.
</td>
</tr>
<tr>
<td>
</td>
<td>
<tt>groups</tt>
</td>
<td>
The list of groups this class/method belongs to.
</td>
</tr>
<tr>
<td>
</td>
<td>
<tt>invocationCount</tt>
</td>
<td>
The number of times this method should be invoked.
</td>
</tr>
<tr>
<td>
</td>
<td>
<tt>invocationTimeOut</tt>
</td>
<td>
The maximum number of milliseconds this test should take for the cumulated time of all the invocationcounts. This attribute will be ignored if invocationCount is not specified.
</td>
</tr>
<tr>
<td>
</td>
<td>
<tt>priority</tt>
</td>
<td>
The priority for this test method. Lower priorities will be scheduled first.
</td>
</tr>
<tr>
<td>
</td>
<td>
<tt>successPercentage</tt>
</td>
<td>
The percentage of success expected from this method
</td>
</tr>
<tr>
<td>
</td>
<td>
<tt>singleThreaded</tt>
</td>
<td>
If set to true, all the methods on this test class are guaranteed to run in the same thread, even if the tests are currently being run with parallel="methods". This attribute can only be used at the class level and it will be ignored if used at the method level. Note: this attribute used to be called <tt>sequential</tt> (now deprecated).
</td>
</tr>
<tr>
<td>
</td>
<td>
<tt>timeOut</tt>
</td>
<td>
The maximum number of milliseconds this test should take.
</td>
</tr>
<tr>
<td>
</td>
<td>
<tt>threadPoolSize</tt>
</td>
<td>
The size of the thread pool for this method.
The method will be invoked from multiple threads as specified by
invocationCount. <br>Note: this attribute is ignored if invocationCount is not specified
</td>
</tr>
</table>
</pre>
<!-------------------------------------
TESTNG.XML
------------------------------------>
<h3><a class="section" name="testng-xml">testng.xml</a></h3>
<p>You can invoke TestNG in several different ways:</p><ul>
<li>With a <tt>testng.xml</tt> file</li><li><a href="http://testng.org/doc/ant.html">With ant</a></li><li>From the command line</li></ul><p>This section describes the format of <tt>testng.xml</tt> (you will find documentation
on ant and the command line below).</p><p>The current DTD for <tt>testng.xml</tt> can be found on the main Web site:
<a href="http://testng.org/testng-1.0.dtd">http://testng.org/testng-1.0.dtd</a>
(for your convenience, you might prefer to browse the
<a href="http://testng.org/dtd">HTML version</a>).</p>
Here is an example <tt>testng.xml</tt> file:
<p>
<h3 class="sourcetitle">testng.xml</h3>
<pre class="brush: xml">
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1" verbose="1" >
<test name="Nopackage" >
<classes>
<class name="NoPackageTest" />
</classes>
</test>
<test name="Regression1">
<classes>
<class name="test.sample.ParameterSample"/>
<class name="test.sample.ParameterTest"/>
</classes>
</test>
</suite>
</pre>
You can specify package names instead of class names:
<h3 class="sourcetitle">testng.xml</h3>
<pre class="brush: xml; highlight: [5,6,7]">
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1" verbose="1" >
<test name="Regression1" >
<packages>
<package name="test.sample" />
</packages>
</test>
</suite>
</pre>
<p>In this example, TestNG will look at all the classes in the package
<tt>test.sample</tt> and will retain only classes that have TestNG annotations.</p>
You can also specify groups and methods to be included and excluded:
<h3 class="sourcetitle">testng.xml</h3>
<pre class="brush: xml">
<test name="Regression1">
<groups>
<run>
<exclude name="brokenTests" />
<include name="checkinTests" />
</run>
</groups>
<classes>
<class name="test.IndividualMethodsTest">
<methods>
<include name="testMethod" />
</methods>
</class>
</classes>
</test>
</pre>
<p>You can also define new groups inside <tt>testng.xml</tt> and specify additional details in attributes, such as whether to run the tests in parallel, how many threads to use, whether you are running JUnit tests, etc...
<p>
By default, TestNG will run your tests in the order they are found in the XML
file. If you want the classes and methods listed in this file to be
run in an unpredictible order, set the <tt>preserve-order</tt>
attribute to <tt>false</tt>
<h3 class="sourcetitle">testng.xml</h3>
<pre class="brush: xml">
<test name="Regression1" preserve-order="false">
<classes>
<class name="test.Test1">
<methods>
<include name="m1" />
<include name="m2" />
</methods>
</class>
<class name="test.Test2" />
</classes>
</test>
</pre>
<p>
Please see the DTD for a complete list of the features, or read on.</p>
<!-------------------------------------
RUNNING TESTNG
------------------------------------>
<h3><a class="section" name="running-testng">Running TestNG</a></h3>
TestNG can be invoked in different ways:
<ul>
<li>Command line
</li>
<li><a href="ant.html">ant</a>
</li>
<li><a href="eclipse.html">Eclipse</a>
</li>
<li><a href="idea.html">IntelliJ's IDEA</a>
</li>
</ul>
This section only explains how to invoke TestNG from the command line. Please click on one of the links above if you are interested in one of the other ways.
<p>
Assuming that you have TestNG in your class path, the simplest way to invoke TestNG is as follows:
<pre class="brush: text">
java org.testng.TestNG testng1.xml [testng2.xml testng3.xml ...]
</pre>
You need to specify at least one XML file describing the TestNG suite you are trying to run. Additionally, the following command-line switches are available:
</p>
<table id="command-line">
<caption>Command Line Parameters</caption><colgroup class="option"/>
<colgroup class="argument"/>
<colgroup class="documentation"/>
<thead>
<tr>
<th>Option</th>
<th>Argument</th>
<th>Documentation</th>
</tr>
</thead>
<tbody>
<tr>
<td>-configfailurepolicy</td>
<td><tt>skip</tt>|<tt>continue</tt></td>
<td>Whether TestNG should <tt>continue</tt> to execute the remaining tests in the suite or <tt>skip</tt> them if
an @Before* method fails. Default behavior is <tt>skip</tt>.</td>
</tr>
<tr>
<td>-d</td>
<td>A directory</td>
<td>The directory where the reports will be generated (defaults to <tt>test-output</tt>).</td>
</tr>
<tr>
<td>-dataproviderthreadcount</td>
<td>The default number of threads to use for data
providers when running tests in parallel.</td>
<td>This sets the default maximum number of threads to use
for data providers when running tests in parallel. It will only take effect if the parallel mode has been selected (for example, with the -parallel option). This can be overridden in the suite definition.</td>
</tr>
<tr>
<td>-excludegroups</td>
<td>A comma-separated list of groups.</td><td>The list of groups you want to be excluded from this run.</td>
</tr>
<tr>
<td>-groups</td>
<td>A comma-separated list of groups.</td>
<td>The list of groups you want to run (e.g. <tt>"windows,linux,regression"</tt>).</td>
</tr>
<tr>
<td>-listener</td>
<td>A comma-separated list of Java classes that can be found on your classpath.</td>
<td>Lets you specify your own test listeners. The classes need to implement <a href="../javadocs/org/testng/ITestListener.html"> <tt>org.testng.ITestListener</tt></a></td>
</tr>
<tr>
<td>-methods</td>
<td>A comma separated list of fully qualified class name and method. For example <tt>com.example.Foo.f1,com.example.Bar.f2</tt>.</td>
<td>Lets you specify individual methods to run.</tt></a></td>
</tr>
<tr>
<td>-methodselectors</td>
<td>A comma-separated list of Java classes and method
priorities that define method selectors.</td>
<td>Lets you specify method selectors on the command
line. For example: <tt>com.example.Selector1:3,com.example.Selector2:2</tt></td>
</tr>
<tr>
<td>-parallel</td>
<td>methods|tests|classes</td>
<td>If specified, sets the default mechanism used to determine how to use parallel threads when running tests. If not set, default mechanism is not to use parallel threads at all. This can be overridden in the suite definition.</td>
</tr>
<tr>
<td>-reporter</td>
<td>The extended configuration for a custom report listener.</td>
<td>Similar to the <tt>-listener</tt> option, except that it allows the configuration of JavaBeans-style properties on the reporter instance.
<br>
Example: <tt>-reporter com.test.MyReporter:methodFilter=*insert*,enableFiltering=true</tt>
<br>
You can have as many occurences of this option, one for each reporter that needs to be added.</td>
</tr>
<tr>
<td>-sourcedir</td>
<td>A semi-colon separated list of directories.</td>
<td>The directories where your javadoc annotated test sources are. This option is only necessary if you are using javadoc type annotations. (e.g. <tt>"src/test"</tt> or <tt>"src/test/org/testng/eclipse-plugin;src/test/org/testng/testng"</tt>).</td>
</tr>
<tr>
<td>-suitename</td>
<td>The default name to use for a test suite.</td>
<td>This specifies the suite name for a test suite defined on the command line. This option is ignored if the suite.xml file or the source code specifies a different suite name. It is possible to create a suite name with spaces in it if you surround it with double-quotes "like this".</td>
</tr>
<tr>
<td>-testclass</td>
<td>A comma-separated list of classes that can be found in your classpath.</td><td>A list of class files separated by commas (e.g. <tt>"org.foo.Test1,org.foo.test2"</tt>).</td>
</tr>
<tr>
<td>-testjar</td>
<td>A jar file.</td>
<td>Specifies a jar file that contains test classes. If a <tt>testng.xml</tt> file is found at the root of that jar file, it will be used, otherwise, all the test classes found in this jar file will be considered test classes.</td>
</tr>
<tr>
<td>-testname</td>
<td>The default name to use for a test.</td>
<td>This specifies the name for a test defined on the command line. This option is ignored if the suite.xml file or the source code specifies a different test name. It is possible to create a test name with spaces in it if you surround it with double-quotes "like this".</td>
</tr>
<tr>
<td>-testnames</td>
<td>A comma separated list of test names.</td>
<td>Only tests defined in a <test> tag matching one of these names will be run.</td>
</tr>
<tr>
<td>-testrunfactory</td>
<td>A Java classes that can be found on your classpath.</td>
<td>Lets you specify your own test runners. The class needs to implement <a href="../javadocs/org/testng/ITestRunnerFactory.html"> <tt>org.testng.ITestRunnerFactory</tt></a>.</td>
</tr>
<tr>
<td>-threadcount</td>
<td>The default number of threads to use when running tests in parallel.</td>
<td>This sets the default maximum number of threads to use for running tests in parallel. It will only take effect if the parallel mode has been selected (for example, with the -parallel option). This can be overridden in the suite definition.</td>
</tr>
<tr>
<td>-xmlpathinjar</td>
<td>The path of the XML file inside the jar file.</td>
<td>This attribute should contain the path to a valid XML file inside the test jar (e.g. <tt>"resources/testng.xml"</tt>). The default is <tt>"testng.xml"</tt>, which means a file called "<tt>testng.xml</tt>" at the root of the jar file. This option will be ignored unless <tt>-testjar</tt> is specified.</td>
</tr>
</tbody>
</table>
<p>
This documentation can be obtained by invoking TestNG without any arguments.
<p>
You can also put the command line switches in a text file, say <tt>c:\command.txt</tt>, and tell TestNG to use that file to retrieve its parameters:
<pre class="brush: text">
C:> more c:\command.txt
-d test-output testng.xml
C:> java org.testng.TestNG @c:\command.txt
</pre>
<p>
Additionally, TestNG can be passed properties on the command line of the Java Virtual Machine, for example
<pre class="brush: text">
java -Dtestng.test.classpath="c:/build;c:/java/classes;" org.testng.TestNG testng.xml
</pre>
Here are the properties that TestNG understands:
<table id="system=properties">
<caption>System properties</caption>
<colgroup class="option"/>
<colgroup class="type"/>
<colgroup class="documentation"/>
<thead>
<tr><th>Property</th>
<th>Type</th>
<th>Documentation</th></tr>
</thead>
<tr>
<td>testng.test.classpath</td>
<td>A semi-colon separated series of directories that contain your test classes.</td>
<td>If this property is set, TestNG will use it to look for your test classes instead of the class path. This is convenient if you are using the <tt>package</tt> tag in your XML file and you have a lot of classes in your classpath, most of them not being test classes.
</tr>
</table>
<br>
<b>Example:</b>
<pre class="brush: text">
java org.testng.TestNG -groups windows,linux -testclass org.test.MyTest
</pre>
The <a href="ant.html">ant task</a> and <a href="#testng-xml">testng.xml</a> allow you to launch TestNG with more parameters (methods to include, specifying parameters, etc...), so you should consider using the command line only when you are trying to learn about TestNG and you want to get up and running quickly.
<p>
<em>Important</em>: The command line flags that specify what tests should be run will be ignored if you also specify a <tt>testng.xml</tt> file, with the exception of <tt>-includedgroups</tt> and <tt>-excludedgroups</tt>, which will override all the group inclusions/exclusions found in <tt>testng.xml</tt>.
<!-------------------------------------
METHODS
------------------------------------>
<h3><a class="section" name="methods">Test methods, Test classes and Test groups</a></h3>
<h4><a class="section" indent=".." name="test-methods">Test methods</a></h4>
Test methods are annotated with <tt>@Test</tt>. Methods annotated with <tt>@Test</tt> that happen to return a value will be ignored, unless you set <tt>allow-return-values</tt> to <tt>true</tt> in your <tt>testng.xml</tt>:
<pre class="brush: xml">
<suite allow-return-values="true">
or
<test allow-return-values="true">
</pre>
<h4><a class="section" indent=".." name="test-groups">Test groups</a></h4>
<p>
TestNG allows you to perform sophisticated groupings of test methods. Not
only can you declare that methods belong to groups, but you can also specify
groups that contain other groups. Then TestNG can be invoked and asked to
include a certain set of groups (or regular expressions) while excluding another
set. This gives you maximum flexibility in how you partition your tests
and doesn't require you to recompile anything if you want to run two different
sets of tests back to back.</p>
<p>
Groups are specified in your <tt>testng.xml</tt> file and can be found either under the <tt><test></tt> or <tt><suite></tt> tag. Groups specified in the <tt><suite></tt> tag apply to all the <tt><test></tt> tags underneath. Note that groups are accumulative in these tags: if you specify group "a" in <tt><suite></tt> and "b" in <tt><test></tt>, then both "a" and "b" will be included.
<p>
<p>For example, it is quite common to have at least two categories of tests</p><ul>
<li>Check-in tests. These tests should be run before you submit new
code. They should typically be fast and just make sure no basic
functionality was broken.<br>
</li>
<li>Functional tests. These tests should cover all the functionalities
of your software and be run at least once a day, although ideally you would
want to run them continuously.</li></ul>
Typically, check-in tests are a subset of functional tests. TestNG
allows you to specify this in a very intuitive way with test groups. For
example, you could structure your test by saying that your entire test class
belongs to the "functest" group, and additionally that a couple of methods
belong to the group "checkintest":
<h3 class="sourcetitle">Test1.java</h3>
<pre class="brush: java">
public class Test1 {
@Test(groups = { "functest", "checkintest" })
public void testMethod1() {
}
@Test(groups = {"functest", "checkintest"} )
public void testMethod2() {
}
@Test(groups = { "functest" })
public void testMethod3() {
}
}
</pre>
Invoking TestNG with
<br>
<h3 class="sourcetitle">testng.xml</h3>
<pre class="brush: xml">
<test name="Test1">
<groups>
<run>
<include name="functest"/>
</run>
</groups>
<classes>
<class name="example1.Test1"/>
</classes>
</test>
</pre>
<p>will run all the test methods in that classes, while invoking it with <tt>checkintest</tt> will only run
<tt>testMethod1()</tt> and <tt>testMethod2()</tt>.</p>
Here is another example, using regular expressions this time. Assume
that some of your test methods should not be run on Linux, your test would look
like:
<h3 class="sourcetitle">Test1.java</h3>
<pre class="brush: java">
@Test
public class Test1 {
@Test(groups = { "windows.checkintest" })
public void testWindowsOnly() {
}
@Test(groups = {"linux.checkintest"} )
public void testLinuxOnly() {
}
@Test(groups = { "windows.functest" )
public void testWindowsToo() {
}
}
</pre>
You could use the following testng.xml to launch only the Windows methods:
<h3 class="sourcetitle">testng.xml</h3>
<pre class="brush: xml; highlight: [4,9]">
<test name="Test1">
<groups>
<run>
<include name="windows.*"/>
</run>
</groups>
<classes>
<class name="example1.Test1"/>
</classes>
</test>
</pre>
<blockquote>
<em>Note: TestNG uses <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expressions</a>, and not <a href="http://en.wikipedia.org/wiki/Wildmat">wildmats</a>. Be aware of the difference (for example, "anything" is matched by ".*" -- dot star -- and not "*").</em>
</blockquote>
<h4><a name="method-groups">Method groups</a></h4>
You can also exclude or include individual methods:
<h3 class="sourcetitle">testng.xml</h3>
<pre class="brush: xml">
<test name="Test1">
<classes>
<class name="example1.Test1">
<methods>
<include name=".*enabledTestMethod.*"/>
<exclude name=".*brokenTestMethod.*"/>
</methods>
</class>
</classes>
</test>
</pre>
This can come in handy to deactivate a single method without having to recompile
anything, but I don't recommend using this technique too much since it makes
your testing framework likely to break if you start refactoring your Java code
(the regular expressions used in the tags might not match your methods any
more).
<h4><a class="section" indent=".." name="groups-of-groups">Groups of groups</a></h4>
Groups can also include other groups. These groups are called "MetaGroups".
For example, you might want to define a group "all" that includes "checkintest"
and "functest". "functest" itself will contain the groups "windows" and
"linux" while "checkintest will only contain "windows". Here is how you
would define this in your property file:
<h3 class="sourcetitle">testng.xml</h3>
<pre class="brush: xml">
<test name="Regression1">
<groups>
<define name="functest">
<include name="windows"/>
<include name="linux"/>
</define>
<define name="all">
<include name="functest"/>
<include name="checkintest"/>
</define>
<run>
<include name="all"/>
</run>
</groups>
<classes>
<class name="test.sample.Test1"/>
</classes>
</test>
</pre>
</p><!-------------------------------------
EXCLUSION
------------------------------------>
<h4><a class="section" indent=".." name="exclusions">Exclusion groups</a></h4>
<p>TestNG allows you to include groups as well as exclude them.</p>
For example, it is quite usual to have tests that temporarily break because
of a recent change, and you don't have time to fix the breakage yet. 4
However, you do want to have clean runs of your functional tests, so you need to
deactivate these tests but keep in mind they will need to be reactivated.</p><p>A simple way to solve this problem is to create a group called "broken" and
make these test methods belong to it. For example, in the above example, I
know that testMethod2() is now broken so I want to disable it:
<h3 class="sourcetitle">Java</h3>
<pre class="brush: java">
@Test(groups = {"checkintest", "broken"} )
public void testMethod2() {
}
</pre>
All I need to do now is to exclude this group from the run:
<h3 class="sourcetitle">testng.xml</h3>
<pre class="brush: xml; highlight: 5">
<test name="Simple example">
<groups>
<run>
<include name="checkintest"/>
<exclude name="broken"/>
</run>
</groups>
<classes>
<class name="example1.Test1"/>
</classes>
</test>
</pre>
<p>This way, I will get a clean test run while keeping track of what tests are
broken and need to be fixed later.</p>
<blockquote>
<p><i>Note: you can also disable tests on an individual basis by using the
"enabled" property available on both @Test and @Before/After
annotations.</i></p>
</blockquote>
<!-------------------------------------
PARTIAL GROUPS
------------------------------------>
<h4><a class="section" indent=".." name="partial-groups">Partial groups</a></h4>
You can define groups at the class level and then add groups at the method level:
<h3 class="sourcetitle">All.java</h3>
<pre class="brush: java">
@Test(groups = { "checkin-test" })
public class All {
@Test(groups = { "func-test" )
public void method1() { ... }
public void method2() { ... }
}
</pre>
In this class, method2() is part of the group "checkin-test", which is defined
at the class level, while method1() belongs to both "checkin-test" and
"func-test".
<!-------------------------------------
PARAMETERS
------------------------------------>
<h4><a class="section" indent=".." name="parameters">Parameters</a></h4>
<p>
Test methods don't have to be parameterless. You can use an arbitrary
number of parameters on each of your test method, and you instruct TestNG to
pass you the correct parameters with the <tt>@Parameters</tt> annotation.</p><p>
There are two ways to set these parameters: with <tt>testng.xml</tt> or
programmatically.</p>
<h5><a class="section" indent="..." name="parameters-testng-xml">Parameters from <tt>testng.xml</tt></a></h5>
If you are using simple values for your parameters, you can specify them in your
<tt>testng.xml</tt>:
<h3 class="sourcetitle">Java</h3>
<pre class="brush: java">
@Parameters({ "first-name" })
@Test
public void testSingleString(String firstName) {
System.out.println("Invoked testString " + firstName);
assert "Cedric".equals(firstName);
}
</pre>
In this code, we specify that the parameter <tt>firstName</tt> of your Java method
should receive the value of the XML parameter called <tt>first-name</tt><i>.</i>
This XML parameter is defined in <tt>testng.xml</tt>:<p>
<h3 class="sourcetitle">testng.xml</h3>
<pre class="brush: xml">
<suite name="My suite">
<parameter name="first-name" value="Cedric"/>
<test name="Simple example">
<-- ... -->
</pre>
<h4><span style="font-weight: 400">The same technique can be used for <tt>@Before/After </tt>and <tt>@Factory</tt> annotations:</span></h4>
<pre class="brush: java">
@Parameters({ "datasource", "jdbcDriver" })
@BeforeMethod
public void beforeTest(String ds, String driver) {
m_dataSource = ...; // look up the value of datasource
m_jdbcDriver = driver;
}
</pre>
This time, the two Java parameter <i>ds</i>
and <i>driver</i> will receive the value given to the properties <tt>datasource</tt>
and <tt>jdbc-driver </tt>respectively.
<p>
Parameters can be declared optional with the <a href="../javadocs/org/testng/annotations/Optional.html"><tt>Optional</tt></a> annotation:
<pre class="brush: java">
@Parameters("db")
@Test
public void testNonExistentParameter(@Optional("mysql") String db) { ... }
</pre>
If no parameter named "db" is found in your <tt>testng.xml</tt> file, your test method will receive the default value specified inside the <tt>@Optional</tt> annotation: "mysql".
<p>The <tt>@Parameters</tt> annotation can be placed at the following locations:</p><ul>
<li>On any method that already has a <tt>@Test</tt>, <tt>@Before/After</tt>
or <tt>@Factory</tt> annotation.</li><li>On at most one constructor of your test class. In this case,
TestNG will invoke this particular constructor with the parameters
initialized to the values specified in <tt>testng.xml</tt> whenever it needs
to instantiate your test class. This feature can be used to initialize fields
inside your classes to values that will then be used by your
test methods.</li></ul>
<blockquote>
<p><i>Notes:
</i>
<ul>
<li><i>The XML parameters are mapped to the Java parameters in the same order as
they are found in the annotation, and TestNG will issue an error if the numbers
don't match. </i>
<li><i>Parameters are scoped. In <tt>testng.xml</tt>, you can declare them either under a
<tt><suite></tt> tag or under <tt><test></tt>. If two parameters have the same name, it's the one
defined in <tt><test></tt> that has precedence. This is convenient if you need to specify
a parameter applicable to all your tests and override its value only for certain
tests. </i>
</ul>
<p></p>
</blockquote>
<h5><a class="section" indent="..." name="parameters-dataproviders">Parameters with DataProviders</a></h5>
<p>Specifying parameters in <tt>testng.xml</tt> might not be sufficient if you need to pass complex parameters, or parameters that need to be created from Java (complex objects, objects read from a property file or a database, etc...). In this case, you can use a Data Provider to supply the values you need to test. A Data Provider is a method on your class that returns an array of array of objects. This method is annotated with <tt>@DataProvider</tt>:
<h3 class="sourcetitle">Java</h3>
<pre class="brush: java">
//This method will provide data to any test method that declares that its Data Provider
//is named "test1"
@DataProvider(name = "test1")
public Object[][] createData1() {
return new Object[][] {
{ "Cedric", new Integer(36) },
{ "Anne", new Integer(37)},
};
}
//This test method declares that its data should be supplied by the Data Provider
//named "test1"
@Test(dataProvider = "test1")
public void verifyData1(String n1, Integer n2) {
System.out.println(n1 + " " + n2);
}
</pre>
will print
<pre class="brush: text">
Cedric 36
Anne 37
</pre>
A <tt>@Test</tt> method specifies its Data Provider with the <tt>dataProvider</tt> attribute.
This name must correspond to a method on the same class annotated with <tt>@DataProvider(name="...")</tt>
with a matching name.
<p>
By default, the data provider will be looked for in the current test class or one of its base classes. If you want to put your data provider in a different class, it needs to be a static method or a class with a non-arg constructor, and you specify the class where it can be found in the <tt>dataProviderClass</tt> attribute:
<h3 class="sourcetitle">StaticProvider.java</h3>
<pre class="brush: java">
public class StaticProvider {
@DataProvider(name = "create")
public static Object[][] createData() {
return new Object[][] {
new Object[] { new Integer(42) }
};
}
}
public class MyTest {
@Test(dataProvider = "create", dataProviderClass = StaticProvider.class)
public void test(Integer n) {
// ...
}
}
</pre>
The data provider supports injection too. TestNG will use the test context for the injection.
The Data Provider method can return one of the following two types:
<ul>
<li>An array of array of objects (<tt>Object[][]</tt>) where the first dimension's size is the number of times the test method will be invoked and the second dimension size contains an array of objects that must be compatible with the parameter types of the test method. This is the cast illustrated by the example above.</li><li>An <tt>Iterator<Object[]></tt>. The only difference with <tt>Object[][]</tt> is that an <tt>Iterator</tt> lets you create your test data lazily. TestNG will invoke the iterator and then the test method with the parameters returned by this iterator one by one. This is particularly useful if you have a lot of parameter sets to pass to the method and you don't want to create all of them upfront.
</ul>
Here is an example of this feature:
<pre class="brush: java">
@DataProvider(name = "test1")
public Iterator<Object[]> createData() {
return new MyIterator(DATA);
}
</pre>
If you declare your <tt>@DataProvider</tt> as taking a <tt>java.lang.reflect.Method</tt>
as first parameter, TestNG will pass the current test method for this
first parameter. This is particularly useful when several test methods
use the same <tt>@DataProvider</tt> and you want it to return different
values depending on which test method it is supplying data for.
<p>
For example, the following code prints the name of the test method inside its <tt>@DataProvider</tt>:
<pre class="brush: java">
@DataProvider(name = "dp")
public Object[][] createData(Method m) {
System.out.println(m.getName()); // print test method name
return new Object[][] { new Object[] { "Cedric" }};
}
@Test(dataProvider = "dp")
public void test1(String s) {
}
@Test(dataProvider = "dp")
public void test2(String s) {
}
</pre>
and will therefore display:
<pre class="brush: text">
test1
test2
</pre>
Data providers can run in parallel with the attribute <tt>parallel</tt>:
<pre class="brush: java">
@DataProvider(parallel = true)
// ...
</pre>
Parallel data providers running from an XML file share the same pool of threads, which has a size of 10 by default. You can modify this value in the <tt><suite></tt> tag of your XML file:
<pre class="brush: xml">
<suite name="Suite1" data-provider-thread-count="20" >
...
</pre>
If you want to run a few specific data providers in a different thread pool, you need to run them from a different XML file.
<p>
<h5><a class="section" indent="..." name="parameters-reports">Parameters in reports</a></h5>
<p>
Parameters used to invoke your test methods are shown in the HTML reports generated by TestNG. Here is an example:
<p align="center">
<img src="pics/parameters.png" />
</p>
</p>
<!-------------------------------------
DEPENDENCIES
------------------------------------>
<h4><a class="section" indent=".." name="dependent-methods">Dependencies</a></h4>
<p>Sometimes, you need
your test methods to be invoked in a certain order. Here are a
few examples:
<ul>
<li>To make sure a certain number of test methods have completed and succeeded
before running more test methods.
<li>To initialize your tests while wanting this initialization methods to be
test methods as well (methods tagged with <tt>@Before/After</tt> will not be part of the
final report).
</ul>
TestNG allows you to specify dependencies either with annotations or
in XML.
<h5><a class="section" indent="..." name="dependencies-with-annotations">Dependencies with annotations</a></h5>
<p>You can use the attributes <tt>dependsOnMethods</tt> or <tt>dependsOnGroups</tt>, found on the <tt>@Test</tt> annotation.</p>There are two kinds of dependencies:
<ul>
<li><b>Hard dependencies</b>. All the methods you depend on must have run and succeeded for you to run. If at least one failure occurred in your dependencies, you will not be invoked and marked as a SKIP in the report.
</li>
<li><b>Soft dependencies</b>. You will always be run after the methods you depend on, even if some of them have failed. This is useful when you just want to make sure that your test methods are run in a certain order but their success doesn't really depend on the success of others. A soft dependency is obtained by adding <tt>"alwaysRun=true"</tt> in your <tt>@Test</tt> annotation.
</ul>
Here is an example of a hard dependency:
<pre class="brush: java">
@Test
public void serverStartedOk() {}
@Test(dependsOnMethods = { "serverStartedOk" })
public void method1() {}
</pre>
<p>In this example, <tt>method1()</tt> is declared as depending on method
serverStartedOk(), which guarantees that serverStartedOk()
will always be invoked first.</p><p>You can also have methods that depend on entire groups:</p>
<pre class="brush: java">
@Test(groups = { "init" })
public void serverStartedOk() {}
@Test(groups = { "init" })
public void initEnvironment() {}
@Test(dependsOnGroups = { "init.*" })
public void method1() {}
</pre>
<p>In this example, method1() is declared as depending on any group matching the
regular expression "init.*", which guarantees that the methods <tt>serverStartedOk()</tt>
and <tt>initEnvironment()</tt> will always be invoked before <tt>method1()</tt>. </p>
<blockquote>
<p><i>Note: as stated before, the order of invocation for methods that
belong in the same group is not guaranteed to be the same across test runs.</i></p></blockquote><p>If a method depended upon fails and you have a hard dependency on it (<tt>alwaysRun=false</tt>, which is the default), the methods that depend on it are <b>not</b>
marked as <tt>FAIL</tt> but as <tt>SKIP</tt>. Skipped methods will be reported as such in
the final report (in a color that is neither red nor green in HTML),
which is important since skipped methods are not necessarily failures.</p><p>Both <tt>dependsOnGroups</tt> and <tt>dependsOnMethods</tt> accept regular
expressions as parameters. For <tt>dependsOnMethods</tt>, if you are
depending on a method which happens to have several overloaded versions, all the
overloaded methods will be invoked. If you only want to invoke one of the
overloaded methods, you should use <tt>dependsOnGroups</tt>.</p><p>For a more advanced example of dependent methods, please refer to
<a href="http://beust.com/weblog/archives/000171.html">this article</a>, which
uses inheritance to provide an elegant solution to the problem of multiple
dependencies.</p>
By default, dependent methods are grouped by class. For example, if method <tt>b()</tt> depends on method <tt>a()</tt> and you have several instances of the class that contains these methods (because of a factory of a data provider), then the invocation order will be as follows:
<pre class="brush: plain">
a(1)
a(2)
b(2)
b(2)
</pre>
TestNG will not run <tt>b()</tt> until all the instances have invoked their <tt>a()</tt> method.
<p>
This behavior might not be desirable in certain scenarios, such as for example testing a sign in and sign out of a web browser for various countries. In such a case, you would like the following ordering:
<pre class="brush: plain">
signIn("us")
signOut("us")
signIn("uk")
signOut("uk")
</pre>
For this ordering, you can use the XML attribute <tt>group-by-instances</tt>. This attribute is valid either on <suite> or <test>:
<pre class="brush: xml">
<suite name="Factory" group-by-instances="true">
or
<test name="Factory" group-by-instances="true">
</pre>
<h5><a class="section" indent="..." name="dependencies-in-xml">Dependencies in XML</a></h5>
Alternatively, you can specify your group dependencies in the <tt>testng.xml</tt> file. You use the <tt><dependencies></tt> tag to achieve this:
<pre class="brush: xml">
<test name="My suite">
<groups>
<dependencies>
<group name="c" depends-on="a b" />
<group name="z" depends-on="c" />
</dependencies>
</groups>
</test>
</pre>
The <tt><depends-on></tt> attribute contains a space-separated list of groups.
<!-------------------------------------
FACTORIES
------------------------------------>
<h4><a class="section" indent=".." name="factories">Factories</a></h4>
Factories allow you to create tests dynamically. For example, imagine you
want to create a test method that will access a page on a Web site several
times, and you want to invoke it with different values:
<h3 class="sourcetitle">TestWebServer.java</h3>
<pre class="brush: java">
public class TestWebServer {
@Test(parameters = { "number-of-times" })
public void accessPage(int numberOfTimes) {
while (numberOfTimes-- > 0) {
// access the web page
}
}
}
</pre>
<h3 class="sourcetitle">testng.xml</h3>
<pre class="brush: java">
<test name="T1">
<parameter name="number-of-times" value="10"/>
<class name= "TestWebServer" />
</test>
<test name="T2">
<parameter name="number-of-times" value="20"/>
<class name= "TestWebServer"/>
</test>
<test name="T3">
<parameter name="number-of-times" value="30"/>
<class name= "TestWebServer"/>
</test>
</pre>
This can become quickly impossible to manage, so instead, you should use a factory:
<h3 class="sourcetitle">WebTestFactory.java</h3>
<pre class="brush: java">
public class WebTestFactory {
@Factory
public Object[] createInstances() {
Object[] result = new Object[10];
for (int i = 0; i < 10; i++) {
result[i] = new WebTest(i * 10);
}
return result;
}
}
</pre>
and the new test class is now:
<h3 class="sourcetitle">WebTest.java</h3>
<pre class="brush: java">
public class WebTest {
private int m_numberOfTimes;
public WebTest(int numberOfTimes) {
m_numberOfTimes = numberOfTimes;
}
@Test
public void testServer() {
for (int i = 0; i < m_numberOfTimes; i++) {
// access the web page
}
}
}
</pre>
<p>Your <tt>testng.xml</tt> only needs to reference the class that
contains the factory method, since the test instances themselves will be created
at runtime:</p>
<pre class="brush: java">
<class name="WebTestFactory" />
</pre>
<p>The factory method can receive parameters just like <tt>@Test</tt> and <tt>@Before/After</tt> and it must return <tt>Object[]</tt>.
The objects returned can be of any class (not necessarily the same class as the
factory class) and they don't even need to contain TestNG annotations (in which
case they will be ignored by TestNG).</p>
<p>
Factories can also be used with data providers, and you can leverage this functionality by putting the <tt>@Factory</tt> annotation either on a regular method or on a constructor. Here is an example of a constructor factory:
<pre class="brush:java">
@Factory(dataProvider = "dp")
public FactoryDataProviderSampleTest(int n) {
super(n);
}
@DataProvider
static public Object[][] dp() {
return new Object[][] {
new Object[] { 41 },
new Object[] { 42 },
};
}
</pre>
The example will make TestNG create two test classes, on with the constructor invoked with the value 41 and the other with 42.
<!-------------------------------------
CLASS LEVEL ANNOTATIONS
------------------------------------>
<h4><a class="section" indent=".." name="class-level">Class level annotations</a></h4>
The <tt>@Test</tt> annotation can be put on a class instead of a test method:
<h3 class="sourcetitle">Test1.java</h3>
<pre class="brush: java">
@Test
public class Test1 {
public void test1() {
}
public void test2() {
}
}
</pre>
The effect of a class level <tt>@Test</tt> annotation is to make all the public methods of this class to become test methods even if they are not annotated. You can still repeat the <tt>@Test</tt> annotation on a method if you want to add certain attributes.
<p>
For example:
<h3 class="sourcetitle">Test1.java</h3>
<pre class="brush: java">
@Test
public class Test1 {
public void test1() {
}
@Test(groups = "g1")
public void test2() {
}
}
</pre>
will make both <tt>test1()</tt> and <tt>test2()</tt> test methods but on top of that, <tt>test2()</tt> now belongs to the group "g1".
<p>
<!-------------------------------------
PARALLEL RUNNING
------------------------------------>
<h4><a class="section" indent=".." name="parallel-running">Parallelism and time-outs</a></h4>
You can instruct TestNG to run your tests in separate threads in various ways.
<h5><a class="section" indent="..." name="parallel-suites">Parallel suites</a></h5>
This is useful if you are running several suite files (e.g. "<tt>java org.testng.TestNG testng1.xml testng2.xml"</tt>) and you want each of these suites to be run in a separate thread. You can use the following command line flag to specify the size of a thread pool:
<pre class="brush: plain">
java org.testng.TestNG -suitethreadpoolsize 3 testng1.xml testng2.xml testng3.xml
</pre>
The corresponding ant task name is <tt>suitethreadpoolsize</tt>.
<h5><a class="section" indent="..." name="parallel-tests">Parallel tests, classes and methods</a></h5>
The <i>parallel</i> attribute on the <suite> tag can take one of following values:
<pre class="brush: xml">
<suite name="My suite" parallel="methods" thread-count="5">
</pre>
<pre class="brush: xml">
<suite name="My suite" parallel="tests" thread-count="5">
</pre>
<pre class="brush: xml">
<suite name="My suite" parallel="classes" thread-count="5">
</pre>
<pre class="brush: xml">
<suite name="My suite" parallel="instances" thread-count="5">
</pre>
<ul>
<li>
<b><tt>parallel="methods"</tt></b>: TestNG will run all your test methods in separate threads. Dependent methods will also run in separate threads but they will respect the order that you specified.
</li>
<br>
<li>
<b><tt>parallel="tests"</tt></b>: TestNG will run all the methods in the same <test> tag in the same thread, but each <test> tag will be in a separate thread. This allows you to group all your classes that are not thread safe in the same <test> and guarantee they will all run in the same thread while taking advantage of TestNG using as many threads as possible to run your tests.
</li>
<br>
<li>
<b><tt>parallel="classes"</tt></b>: TestNG will run all the methods in the same class in the same thread, but each class will be run in a separate thread.
</li>
<br/>
<li>
<b><tt>parallel="instances"</tt></b>: TestNG will run all the methods in the same instance in the same thread, but two methods on two different instances will be running in different threads.
</li>
</ul>
<p>
Additionally, the attribute <i>
thread-count</i> allows you to specify how many threads should be allocated for
this execution.<blockquote>
<p><i>Note: the <tt>@Test</tt> attribute <tt>timeOut</tt> works in both
parallel and non-parallel mode.</i></p></blockquote>You can also specify that a <tt>@Test</tt> method should be invoked from different threads. You can use the attribute <tt>threadPoolSize</tt> to achieve this result:
<pre class="brush: java">
@Test(threadPoolSize = 3, invocationCount = 10, timeOut = 10000)
public void testServer() {
</pre>
In this example, the function <tt>testServer</tt> will be invoked ten times from three different threads. Additionally, a time-out of ten seconds guarantees that none of the threads will block on this thread forever.
<!-------------------------------------
RERUNNING
------------------------------------>
<h4><a class="section" indent=".." name="rerunning">Rerunning failed tests</a></h4>
Every time tests fail in a suite, TestNG creates a file called <tt>testng-failed.xml</tt> in the output directory.
This XML file contains the necessary information to rerun only these methods
that failed, allowing you to quickly reproduce the failures without having to
run the entirety of your tests. Therefore, a typical session would look
like this:
<pre class="brush: text">
java -classpath testng.jar;%CLASSPATH% org.testng.TestNG -d test-outputs testng.xml
java -classpath testng.jar;%CLASSPATH% org.testng.TestNG -d test-outputs test-outputs\testng-failed.xml
</pre>
<p>Note that <tt>testng-failed.xml</tt> will contain all the necessary dependent
methods so that you are guaranteed to run the methods that failed without any
SKIP failures.</p>
<h4><a class="section" indent=".." name="junit">JUnit tests</a></h4>
TestNG can run JUnit 3 and JUnit 4 tests. All you need to do is
put the JUnit jar file on the classpath, specify your JUnit test classes in the <tt>testng.classNames</tt>
property and set the <tt>testng.junit</tt> property to true:
<p></p>
<h3 class="sourcetitle">testng.xml</h3>
<pre class="brush: xml">
<test name="Test1" junit="true">
<classes>
<!-- ... -->
</pre>
<p>The behavior of TestNG in this case is similar to JUnit depending on the JUnit version found on the class path:<br>
</p>
<ul>
<li>JUnit 3:
<ul>
<li>All methods starting with test* in your classes will be run</li><li>If there is a method setUp() on your test class, it will be invoked before
every test method</li><li>If there is a method tearDown() on your test class, it will be invoked
before after every test method</li><li>If your test class contains a method suite(), all the tests returned by
this method will be invoked</li></ul>
</li>
<li>JUnit 4:
<ul>
<li>TestNG will use the <tt>org.junit.runner.JUnitCore</tt> runner to run your tests</li>
</ul>
</li>
</ul>
<!-------------------------------------
JUNIT
--------------------------------------->
<!-------------------------------------
RUNNING TESTNG
------------------------------------>
<h4><a class="section" indent=".." name="running-testng-programmatically">Running TestNG programmatically</a></h4>
You can invoke TestNG from your own programs very easily:
<h3 class="sourcetitle">Java</h3>
<pre class="brush: java">
TestListenerAdapter tla = new TestListenerAdapter();
TestNG testng = new TestNG();
testng.setTestClasses(new Class[] { Run2.class });
testng.addListener(tla);
testng.run();
</pre>
This example creates a <tt><a href="http://testng.org/javadocs/org/testng/TestNG.html">TestNG</a></tt> object and runs the test class <tt>Run2</tt>. It also adds a <tt>TestListener</tt>. You can either use the adapter class <tt><a href="http://testng.org/javadocs/org/testng/TestListenerAdapter.html">org.testng.TestListenerAdapter</a></tt> or implement <tt><a href="http://testng.org/javadocs/org/testng/ITestListener.html">org.testng.ITestListener</a></tt> yourself. This interface contains various callback methods that let you keep track of when a test starts, succeeds, fails, etc...
<p>
Similary, you can invoke TestNG on a <tt>testng.xml</tt> file or you can create a virtual <tt>testng.xml</tt> file yourself. In order to do this, you can use the classes found the package <tt><a href="http://testng.org/javadocs/org/testng/xml/package-frame.html">org.testng.xml</a></tt>: <tt><a href="http://testng.org/javadocs/org/testng/xml/XmlClass.html">XmlClass</a></tt>, <tt><a href="http://testng.org/javadocs/org/testng/xml/XmlTest.html">XmlTest</a></tt>, etc... Each of these classes correspond to their XML tag counterpart.
<p>
For example, suppose you want to create the following virtual file:
<pre class="brush: java">
<suite name="TmpSuite" >
<test name="TmpTest" >
<classes>
<class name="test.failures.Child" />
<classes>
</test>
</suite>
</pre>
You would use the following code:
<pre class="brush: java">
XmlSuite suite = new XmlSuite();
suite.setName("TmpSuite");
XmlTest test = new XmlTest(suite);
test.setName("TmpTest");
List<XmlClass> classes = new ArrayList<XmlClass>();
classes.add(new XmlClass("test.failures.Child"));
test.setXmlClasses(classes) ;
</pre>
And then you can pass this <tt>XmlSuite</tt> to TestNG:
<pre class="brush: java">
List<XmlSuite> suites = new ArrayList<XmlSuite>();
suites.add(suite);
TestNG tng = new TestNG();
tng.setXmlSuites(suites);
tng.run();
</pre>
<p>Please see the <a href="../doc/javadocs/org/testng/package-summary.html" target="mainFrame">JavaDocs</a> for the entire API.</p><p>
<!-------------------------------------
BEANSHELL
------------------------------------>
<h4><a class="section" indent=".." name="beanshell">BeanShell and advanced group selection</a></h4>
<p>If the <tt><include></tt> and <tt><exclude></tt> tags in <tt>testng.xml</tt> are not enough for your needs, you can use a <a href="http://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 <tt><test></tt> tag:</p>
<h3 class="sourcetitle">testng.xml</h3>
<pre class="brush: xml">
<test name="BeanShell test">
<method-selectors>
<method-selector>
<script language="beanshell"><![CDATA[
groups.containsKey("test1")
]]></script>
</method-selector>
</method-selectors>
<!-- ... -->
</pre>
When a <tt><script></tt> tag is found in <tt>testng.xml</tt>, TestNG will ignore subsequent <tt><include></tt> and <tt><exclude></tt> of groups and methods in the current <tt><test></tt> tag: your BeanShell expression will be the only way to decide whether a test method is included or not.</p><p>Here are additional information on the BeanShell script:</p><ul>
<li>
It must return a boolean value. Except for this constraint, any valid BeanShell code is allowed (for example, you might want to return <tt>true </tt>during week days and false during weekends, which would allow you to run tests differently depending on the date).<br>
</li>
<li>
TestNG defines the following variables for your convenience:<br> <b><tt>java.lang.reflect.Method method</tt></b>: the current test method.<br> <b>org.testng.ITestNGMethod testngMethod</b>: the description of the current test method.<br> <b><tt>java.util.Map<String, String> groups</tt></b>: a map of the groups the current test method belongs to.<br>
</li>
<li>
You might want to surround your expression with a <tt>CDATA</tt> declaration (as shown above) to avoid tedious quoting of reserved XML characters).<br>
</li>
</ul>
<!-------------------------------------
ANNOTATION TRANSFORMERS
------------------------------------>
<h4><a class="section" indent=".." name="annotationtransformers">Annotation Transformers</a></h4>
TestNG allows you to modify the content of all the annotations at runtime. This is especially useful if the annotations in the source code are right most of the time, but there are a few situations where you'd like to override their value.
<p>
In order to achieve this, you need to use an Annotation Transformer.
<p>
An Annotation Transformer is a class that implements the following interface:
<pre class="brush: java">
public interface IAnnotationTransformer {
/**
* This method will be invoked by TestNG to give you a chance
* to modify a TestNG annotation read from your test classes.
* You can change the values you need by calling any of the
* setters on the ITest interface.
*
* Note that only one of the three parameters testClass,
* testConstructor and testMethod will be non-null.
*
* @param annotation The annotation that was read from your
* test class.
* @param testClass If the annotation was found on a class, this
* parameter represents this class (null otherwise).
* @param testConstructor If the annotation was found on a constructor,
* this parameter represents this constructor (null otherwise).
* @param testMethod If the annotation was found on a method,
* this parameter represents this method (null otherwise).
*/
public void transform(ITest annotation, Class testClass,
Constructor testConstructor, Method testMethod);
}
</pre>
Like all the other TestNG listeners, you can specify this class either on the command line or with ant:
<p>
<pre class="brush: java">
java org.testng.TestNG -listener MyTransformer testng.xml
</pre>
or programmatically:
<p>
<pre class="brush: java">
TestNG tng = new TestNG();
tng.setAnnotationTransformer(new MyTransformer());
// ...
</pre>
When the method <tt>transform()</tt> is invoked, you can call any of the setters on the <tt>ITest test</tt> parameter to alter its value before TestNG proceeds further.
<p>
For example, here is how you would override the attribute <tt>invocationCount</tt> but only on the test method <tt>invoke()</tt> of one of your test classes:
<pre class="brush: java">
public class MyTransformer implements IAnnotationTransformer {
public void transform(ITest annotation, Class testClass,
Constructor testConstructor, Method testMethod)
{
if ("invoke".equals(testMethod.getName())) {
annotation.setInvocationCount(5);
}
}
}
</pre>
<tt>IAnnotationTransformer</tt> only lets you modify a <tt>@Test</tt> annotation. If you need to modify another TestNG annotation (a configuration annotation, <tt>@Factory</tt> or <tt>@DataProvider</tt>), use an <tt>IAnnotationTransformer2</tt>.
<!-------------------------------------
METHOD INTERCEPTORS
------------------------------------>
<h4><a class="section" indent=".." name="methodinterceptors">Method Interceptors</a></h4>
Once TestNG has calculated in what order the test methods will be invoked, these methods are split in two groups:
<ul>
<li><em>Methods run sequentially</em>. These are all the test methods that have dependencies or dependents. These methods will be run in a specific order.
<li><em>Methods run in no particular order</em>. These are all the methods that don't belong in the first category. The order in which these test methods are run is random and can vary from one run to the next (although by default, TestNG will try to group test methods by class).
</ul>
In order to give you more control on the methods that belong to the second category, TestNG defines the following interface:
<pre class="brush: java">
public interface IMethodInterceptor {
List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context);
}
</pre>
The list of methods passed in parameters are all the methods that can be run in any order. Your <tt>intercept</tt> method is expected to return a similar list of <tt>IMethodInstance</tt>, which can be either of the following:
<ul>
<li>The same list you received in parameter but in a different order.
<li>A smaller list of <tt>IMethodInstance</tt> objects.
<li>A bigger list of <tt>IMethodInstance</tt> objects.
</ul>
Once you have defined your interceptor, you pass it to TestNG as a listener. For example:
<p>
<h3 class="sourcetitle">Shell</h3>
<pre class="brush: text">
java -classpath "testng-jdk15.jar:test/build" org.testng.TestNG -listener test.methodinterceptors.NullMethodInterceptor
-testclass test.methodinterceptors.FooTest
</pre>
For the equivalent <tt>ant</tt> syntax, see the <tt>listeners</tt> attribute in the <a href="ant.html">ant documentation</a>.
<p>
For example, here is a Method Interceptor that will reorder the methods so that test methods that belong to the group "fast" are always run first:
<pre class="brush: java">
public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
List<IMethodInstance> result = new ArrayList<IMethodInstance>();
for (IMethodInstance m : methods) {
Test test = m.getMethod().getConstructorOrMethod().getAnnotation(Test.class);
Set<String> groups = new HashSet<String>();
for (String group : test.groups()) {
groups.add(group);
}
if (groups.contains("fast")) {
result.add(0, m);
}
else {
result.add(m);
}
}
return result;
}
</pre>
<!-------------------------------------
TESTNG LISTENERS
------------------------------------>
<h4><a class="section" indent=".." name="testng-listeners">TestNG Listeners</a></h4>
There are several interfaces that allow you to modify TestNG's behavior. These interfaces are broadly called "TestNG Listeners". Here are a few listeners:
<ul>
<li><tt>IAnnotationTransformer</tt> (<a href="#annotationtransformers">doc</a>, <a href="../javadocs/org/testng/IAnnotationTransformer.html">javadoc</a>)
<li><tt>IAnnotationTransformer2</tt> (<a href="#annotationtransformers">doc</a>, <a href="../javadocs/org/testng/IAnnotationTransformer2.html">javadoc</a>)
<li><tt>IHookable</tt> (<a href="#ihookable">doc</a>, <a href="../javadocs/org/testng/IHookable.html">javadoc</a>)
<li><tt>IInvokedMethodListener</tt> (doc, <a href="../javadocs/org/testng/IInvokedMethodListener.html">javadoc</a>)
<li><tt>IMethodInterceptor</tt> (<a href="#methodinterceptors">doc</a>, <a href="../javadocs/org/testng/IMethodInterceptor.html">javadoc</a>)
<li><tt>IReporter</tt> (<a href="#logging-reporters">doc</a>, <a href="../javadocs/org/testng/IReporter.html">javadoc</a>)
<li><tt>ISuiteListener</tt> (doc, <a href="../javadocs/org/testng/ISuiteListener.html">javadoc</a>)
<li><tt>ITestListener</tt> (<a href="#logging-listeners">doc</a>, <a href="../javadocs/org/testng/ITestListener.html">javadoc</a>)
</ul>
When you implement one of these interfaces, you can let TestNG know about it with either of the following ways:
<ul>
<li><a href="#running-testng">Using -listener on the command line.</a>
<li><a href="ant.html">Using <listeners> with ant.</a>
<li>Using <listeners> in your <tt>testng.xml</tt> file.
<li>Using the <tt>@Listeners</tt> annotation on any of your test classes.
<li>Using <tt>ServiceLoader</tt>.
</ul>
<h5><a class="section" indent="..." name="listeners-testng-xml">Specifying listeners with <tt>testng.xml</tt> or in Java</a></h5>
Here is how you can define listeners in your <tt>testng.xml</tt> file:
<pre class="brush: xml">
<suite>
<listeners>
<listener class-name="com.example.MyListener" />
<listener class-name="com.example.MyMethodInterceptor" />
</listeners>
...
</pre>
Or if you prefer to define these listeners in Java:
<pre class="brush: java">
@Listeners({ com.example.MyListener.class, com.example.MyMethodInterceptor.class })
public class MyTest {
// ...
}
</pre>
The <tt>@Listeners</tt> annotation can contain any class that extends <tt>org.testng.ITestNGListener</tt> <b>except</b> <tt>IAnnotationTransformer</tt> and <tt>IAnnotationTransformer2</tt>. The reason is that these listeners need to be known very early in the process so that TestNG can use them to rewrite your annotations, therefore you need to specify these listeners in your <tt>testng.xml</tt> file.
<p>
Note that the <tt>@Listeners</tt> annotation will apply to your entire suite file, just as if you had specified it in a <tt>testng.xml</tt> file. If you want to restrict its scope (for example, only running on the current class), the code in your listener could first check the test method that's about to run and decide what to do then.
<h5><a class="section" indent="..." name="listeners-service-loader">Specifying listeners with <tt>ServiceLoader</tt></a></h5>
Finally, the JDK offers a very elegant mechanism to specify implementations of interfaces on the class path via the <tt><a href="http://download.oracle.com/javase/6/docs/api/java/util/ServiceLoader.html">ServiceLoader</a></tt> class.
<p>
With ServiceLoader, all you need to do is create a jar file that contains your listener(s) and a few configuration files, put that jar file on the classpath when you run TestNG and TestNG will automatically find them.
<p>
Here is a concrete example of how it works.
<p>
Let's start by creating a listener (any TestNG listener should work):
<pre class="brush: java">
package test.tmp;
public class TmpSuiteListener implements ISuiteListener {
@Override
public void onFinish(ISuite suite) {
System.out.println("Finishing");
}
@Override
public void onStart(ISuite suite) {
System.out.println("Starting");
}
}
</pre>
Compile this file, then create a file at the location <tt>META-INF/services/org.testng.ITestNGListener</tt>, which will name the implementation(s) you want for this interface.
<p>
You should end up with the following directory structure, with only two files:
<pre class="brush: plain; highlight: [4, 7]">
$ tree
|____META-INF
| |____services
| | |____org.testng.ITestNGListener
|____test
| |____tmp
| | |____TmpSuiteListener.class
$ cat META-INF/services/org.testng.ITestNGListener
test.tmp.TmpSuiteListener
</pre>
Create a jar of this directory:
<pre class="brush: plain">
$ jar cvf ../sl.jar .
added manifest
ignoring entry META-INF/
adding: META-INF/services/(in = 0) (out= 0)(stored 0%)
adding: META-INF/services/org.testng.ITestNGListener(in = 26) (out= 28)(deflated -7%)
adding: test/(in = 0) (out= 0)(stored 0%)
adding: test/tmp/(in = 0) (out= 0)(stored 0%)
adding: test/tmp/TmpSuiteListener.class(in = 849) (out= 470)(deflated 44%)
</pre>
Next, put this jar file on your classpath when you invoke TestNG:
<pre class="brush: plain">
$ java -classpath sl.jar:testng.jar org.testng.TestNG testng-single.yaml
Starting
f2 11 2
PASSED: f2("2")
Finishing
</pre>
This mechanism allows you to apply the same set of listeners to an entire organization just by adding a jar file to the classpath, instead of asking every single developer to remember to specify these listeners in their testng.xml file.
<!-------------------------------------
DEPENDENCY INJECTION
------------------------------------>
<h4><a class="section" indent=".." name="dependency-injection">Dependency injection</a></h4>
TestNG supports two different kinds of dependency injection: native (performed by TestNG itself) and external (performed by a dependency injection framework such as Guice).
<h5><a class="section" indent="..." name="native-dependency-injection">Native dependency injection</a></h5>
TestNG lets you declare additional parameters in your methods. When this happens, TestNG will automatically fill these parameters with the right value. Dependency injection can be used in the following places:
<ul>
<li>
Any @Before method or @Test method can declare a parameter of type <tt>ITestContext</tt>.
<li>
Any @AfterMethod method can declare a parameter of type <tt>ITestResult</tt>, which will reflect the result of the test method that was just run.
<li>
Any @Before and @After methods can declare a parameter of type <tt>XmlTest</tt>, which contain the current <tt><test></tt> tag.
<li>
Any @BeforeMethod (and @AfterMethod) can declare a parameter of type
<tt>java.lang.reflect.Method</tt>. This parameter will receive the
test method that will be called once this @BeforeMethod finishes (or
after the method as run for @AfterMethod).
<li>
Any @BeforeMethod can declare a parameter of type <tt>Object[]</tt>. This parameter will receive the list of parameters that are about to be fed to the upcoming test method, which could be either injected by TestNG, such as <tt>java.lang.reflect.Method</tt> or come from a <tt>@DataProvider</tt>.
<li>
Any @DataProvider can declare a parameter of type
<tt>ITestContext</tt> or <tt>java.lang.reflect.Method</tt>. The
latter parameter will receive the test method that is about to be invoked.
</ul>
You can turn off injection with the <tt>@NoInjection</tt> annotation:
<pre class="brush: java; highlight: [9]">
public class NoInjectionTest {
@DataProvider(name = "provider")
public Object[][] provide() throws Exception {
return new Object[][] { { CC.class.getMethod("f") } };
}
@Test(dataProvider = "provider")
public void withoutInjection(@NoInjection Method m) {
Assert.assertEquals(m.getName(), "f");
}
@Test(dataProvider = "provider")
public void withInjection(Method m) {
Assert.assertEquals(m.getName(), "withInjection");
}
}
</pre>
<h5><a class="section" indent="..." name="guice-dependency-injection">Guice dependency injection</a></h5>
If you use Guice, TestNG gives you an easy way to inject your test objects with a Guice module:
<pre class="brush: java">
@Guice(modules = GuiceExampleModule.class)
public class GuiceTest extends SimpleBaseTest {
@Inject
ISingleton m_singleton;
@Test
public void singletonShouldWork() {
m_singleton.doSomething();
}
}
</pre>
In this example, <tt>GuiceExampleModule</tt> is expected to bind the interface <tt>ISingleton</tt> to some concrete class:
<pre class="brush: java">
public class GuiceExampleModule implements Module {
@Override
public void configure(Binder binder) {
binder.bind(ISingleton.class).to(ExampleSingleton.class).in(Singleton.class);
}
}
</pre>
If you need more flexibility in specifying which modules should be used to instantiate your test classes, you can specify a module factory:
<pre class="brush: java">
@Guice(moduleFactory = ModuleFactory.class)
public class GuiceModuleFactoryTest {
@Inject
ISingleton m_singleton;
@Test
public void singletonShouldWork() {
m_singleton.doSomething();
}
}
</pre>
The module factory needs to implement the interface <a href="../javadocs/org/testng/IModuleFactory.html">IModuleFactory</a>:
<pre class="brush: java">
public interface IModuleFactory {
/**
* @param context The current test context
* @param testClass The test class
*
* @return The Guice module that should be used to get an instance of this
* test class.
*/
Module createModule(ITestContext context, Class<?> testClass);
}
</pre>
Your factory will be passed an instance of the test context and the test class that TestNG needs to instantiate. Your <tt>createModule</tt> method should return a Guice Module that will know how to instantiate this test class. You can use the test context to find out more information about your environment, such as parameters specified in <tt>testng.xml</tt>, etc...
You will get even more flexibility and Guice power with <tt>parent-module</tt> and <tt>guice-stage</tt> suite parameters.
<tt>guice-stage</tt> allow you to chose the <a href="https://github.com/google/guice/wiki/Bootstrap"><tt>Stage</tt></a> used to create the parent injector.
The default one is <tt>DEVELOPMENT</tt>. Other allowed values are <tt>PRODUCTION</tt> and <tt>TOOL</tt>.
Here is how you can define parent-module in your test.xml file:
<pre class="brush: xml">
<suite parent-module="com.example.SuiteParenModule" guice-stage="PRODUCTION">
</suite>
</pre>
TestNG will create this module only once for given suite. Will also use this module for obtaining instances of test specific Guice modules and module factories, then will create child injector for each test class.
With such approach you can declare all common bindings in parent-module also you can inject binding declared in parent-module in module and module factory. Here is an example of this functionality:
<pre class="brush: java">
package com.example;
public class ParentModule extends AbstractModule {
@Override
protected void conigure() {
bind(MyService.class).toProvider(MyServiceProvider.class);
bind(MyContext.class).to(MyContextImpl.class).in(Singleton.class);
}
}
</pre>
<pre class="brush: java">
package com.example;
public class TestModule extends AbstractModule {
private final MyContext myContext;
@Inject
TestModule(MyContext myContext) {
this.myContext = myContext
}
@Override
protected void configure() {
bind(MySession.class).toInstance(myContext.getSession());
}
}
</pre>
<pre class="brush: xml">
<suite parent-module="com.example.ParentModule">
</suite>
</pre>
<pre class="brush: java">
package com.example;
@Test
@Guice(modules = TestModule.class)
public class TestClass {
@Inject
MyService myService;
@Inject
MySession mySession;
public void testServiceWithSession() {
myService.serve(mySession);
}
}
</pre>
As you see ParentModule declares binding for MyService and MyContext classes. Then MyContext is injected using constructor injection into TestModule class, which also declare binding for MySession. Then parent-module in test XML file is set to ParentModule class, this enables injection in TestModule. Later in TestClass you see two injections:
* MyService - binding taken from ParentModule
* MySession - binding taken from TestModule
This configuration ensures you that all tests in this suite will be run with same session instance, the MyContextImpl object is only created once per suite, this give you possibility to configure common environment state for all tests in suite.
<!-------------------------------------
INVOKED METHOD LISTENERS
------------------------------------>
<h4><a class="section" indent=".." name="invokedmethodlistener">Listening to method invocations</a></h4>
The listener <tt><a href="../javadocs/org/testng/IInvokedMethodListener.html">IInvokedMethodListener</a></tt> allows you to be notified whenever TestNG is about to invoke a test (annotated with <tt>@Test</tt>) or configuration (annotated with any of the <tt>@Before</tt> or <tt>@After</tt> annotation) method. You need to implement the following interface:
<pre class="brush: java">
public interface IInvokedMethodListener extends ITestNGListener {
void beforeInvocation(IInvokedMethod method, ITestResult testResult);
void afterInvocation(IInvokedMethod method, ITestResult testResult);
}
</pre>
and declare it as a listener, as explained in <a href="#testng-listeners">the section about TestNG listeners</a>.
<p>
<!-------------------------------------
IHOOKABLE AND ICONFIGURABLE
------------------------------------>
<h4><a class="section" indent=".." name="ihookable">Overriding test methods</a></h4>
TestNG allows you to override and possibly skip the invocation of test methods. One example of where this is useful is if you need to your test methods with a specific security manager. You achieve this by providing a listener that implements <a href="../javadocs/org/testng/IHookable.html"><tt>IHookable</tt></a>.
<p>
Here is an example with JAAS:
<pre class="brush: java">
public class MyHook implements IHookable {
public void run(final IHookCallBack icb, ITestResult testResult) {
// Preferably initialized in a @Configuration method
mySubject = authenticateWithJAAs();
Subject.doAs(mySubject, new PrivilegedExceptionAction() {
public Object run() {
icb.callback(testResult);
}
};
}
}
</pre>
<p>
<!-------------------------------------
IALTERSUITELISTENER
------------------------------------>
<h4><a class="section" indent=".." name="ialtersuite">Altering suites (or) tests</a></h4>
Sometimes you may need to just want to alter a suite (or) a test tag in a suite xml in runtime without having to
change the contents of a suite file.
<p>
A classic example for this would be to try and leverage your existing suite file and try using it for simulating a load test
on your "Application under test".
At the minimum you would end up duplicating the contents of your <test> tag multiple
times and create a new suite xml file and work with. But this doesn't seem to scale a lot.
<p>
TestNG allows you to alter a suite (or) a test tag in your suite xml file at runtime via listeners.
You achieve this by providing a listener that implements <a href="../javadocs/org/testng/IAlterSuiteListener.html"><tt>IAlterSuiteListener</tt></a>.
Please refer to <a href="#testng-listeners">Listeners section</a> to learn about listeners.
<p>
Here is an example that shows how the suite name is getting altered in runtime:
<pre class="brush: java">
public class AlterSuiteNameListener implements IAlterSuiteListener {
@Override
public void alter(List<XmlSuite> suites) {
XmlSuite suite = suites.get(0);
suite.setName(getClass().getSimpleName());
}
}
</pre>
This listener can only be added with either of the following ways:
<ul>
<li>Through the <tt><listeners></tt> tag in the suite xml file.</li>
<li>Through a <a href="#listeners-service-loader">Service Loader</a></li>
</ul>
This listener cannot be added to execution using the <tt>@Listeners</tt> annotation.
<!------------------------------------
TEST SUCCESS
------------------------------------>
<h3><a class="section" indent="." name="test-results">Test results</a></h3>
<h4><a class="section" indent=".." name="success-failure">Success, failure and assert</a></h4>
<p>A test is considered successful if it completed without throwing any
exception or if it threw an exception that was expected (see the
documentation for the <tt>expectedExceptions</tt> attribute found on the <tt>@Test</tt> annotation).
</p>
<p>Your test methods will typically be made of calls that can throw an
exception, or of various assertions (using the Java "assert" keyword). An
"assert" failing will trigger an AssertionErrorException, which in turn will
mark the method as failed (remember to use -ea on the JVM if you are not seeing
the assertion errors).</p><p>Here is an example test method:</p>
<pre class="brush: java">
@Test
public void verifyLastName() {
assert "Beust".equals(m_lastName) : "Expected name Beust, for" + m_lastName;
}
</pre>
TestNG also include JUnit's Assert class, which lets you perform
assertions on complex objects:
<pre class="brush: java">
import static org.testng.AssertJUnit.*;
//...
@Test
public void verify() {
assertEquals("Beust", m_lastName);
}
</pre>
<p>Note that the above code use a static import in order to be able to use the
<tt>assertEquals</tt> method without having to prefix it by its class.
<!-------------------------------------
LOGGING
------------------------------------>
</p>
<h4><a class="section" indent=".." name="logging">Logging and results</a></h4>
The results of the test run are created in a file called <tt>index.html</tt> in the
directory specified when launching SuiteRunner. This file points to
various other HTML and text files that contain the result of the entire test
run. You can see a typical example
<a href="http://testng.org/test-output/index.html">here</a>.
<p>
It's very easy to generate your own reports with TestNG with Listeners and Reporters:
<ul>
<li><b>Listeners</b> implement the interface <a href="../javadocs/org/testng/ITestListener.html"><tt>org.testng.ITestListener</tt></a> and are notified in real time of when a test starts, passes, fails, etc...</li><li><b>Reporters</b> implement the interface <a href="../javadocs/org/testng/IReporter.html"><tt>org.testng.IReporter</tt></a> and are notified when all the suites have been run by TestNG. The IReporter instance receives a list of objects that describe the entire test run.</li></ul>For example, if you want to generate a PDF report of your test run, you don't need to be notified in real time of the test run so you should probably use an <tt>IReporter</tt>. If you'd like to write a real-time reporting of your tests, such as a GUI with a progress bar or a text reporter displaying dots (".") as each test is invoked (as is explained below), <tt>ITestListener</tt> is your best choice.
<h5><a class="section" indent="..." name="logging-listeners">Logging Listeners</a></h5>
Here is a listener that displays a "." for each passed test, a "F" for each failure and a "S" for each skip:
<pre class="brush: java">
public class DotTestListener extends TestListenerAdapter {
private int m_count = 0;
@Override
public void onTestFailure(ITestResult tr) {
log("F");
}
@Override
public void onTestSkipped(ITestResult tr) {
log("S");
}
@Override
public void onTestSuccess(ITestResult tr) {
log(".");
}
private void log(String string) {
System.out.print(string);
if (++m_count % 40 == 0) {
System.out.println("");
}
}
}
</pre>
In this example, I chose to extend <a href="../javadocs/org/testng/TestListenerAdapter.html"><tt>TestListenerAdapter</tt></a>, which implements <a href="../javadocs/org/testng/ITestListener.html"><tt>ITestListener</tt></a> with empty methods, so I don't have to override other methods from the interface that I have no interest in. You can implement the interface directly if you prefer.
<p>
Here is how I invoke TestNG to use this new listener:
<h3 class="sourcetitle">Shell</h3>
<pre class="brush: text">
java -classpath testng.jar;%CLASSPATH% org.testng.TestNG -listener org.testng.reporters.DotTestListener test\testng.xml
</pre>
and the output:
<p>
<h3 class="sourcetitle">Shell</h3>
<pre class="brush: text">
........................................
........................................
........................................
........................................
........................................
.........................
===============================================
TestNG JDK 1.5
Total tests run: 226, Failures: 0, Skips: 0
===============================================
</pre>
Note that when you use <tt>-listener</tt>, TestNG will automatically determine the type of listener you want to use.
<h5><a class="section" indent="..." name="logging-reporters">Logging Reporters</a></h5>
The <a href="../javadocs/org/testng/IReporter.html"><tt>org.testng.IReporter</tt></a> interface only has one method:
<pre class="brush: java">
public void generateReport(List<ISuite</a>> suites, String outputDirectory)
</pre>
This method will be invoked by TestNG when all the suites have been run and you can inspect its parameters to access all the information on the run that was just completed.
<p>
<h5><a class="section" indent="..." name="logging-junitreports">JUnitReports</a></h5>
<p>
TestNG contains a listener that takes the TestNG results
and outputs an XML file that can then be fed to JUnitReport. <a href="http://testng.org/test-report/junit-noframes.html">
Here</a> is an example, and the ant task to create this report:
<h3 class="sourcetitle">build.xml</h3>
<pre class="brush: xml">
<target name="reports">
<junitreport todir="test-report">
<fileset dir="test-output">
<include name="*/*.xml"/>
</fileset>
<report format="noframes" todir="test-report"/>
</junitreport>
</target>
</pre>
<blockquote>
<em>Note: a current incompatibility between the JDK 1.5 and JUnitReports
prevents the frame version from working, so you need to specify "noframes" to
get this to work for now.</em>
</blockquote>
<h5><a class="section" indent="..." name="logging-reporter-api">Reporter API</a></h5>
<p>
If you need to log messages that should appear in the generated HTML reports, you can use the class <tt><a href="../javadocs/org/testng/Reporter.html">org.testng.Reporter</a></tt>:
<blockquote class="brush: text">
<font color="#ffffff"> </font><font color="#000000">Reporter.log</font><font color="#000000">(</font><font color="#2a00ff">"M3 WAS CALLED"</font><font color="#000000">)</font><font color="#000000">;</font>
</blockquote>
<p align="center">
<img src="pics/show-output1.png" />
<img src="pics/show-output2.png" />
</p>
<h5><a class="section" indent="..." name="logging-xml-reports">XML Reports</a></h5>
<p>
TestNG offers an XML reporter capturing TestNG specific information that is not available in JUnit reports. This is particulary useful when the user's test environment needs to consume XML results with TestNG-specific data that the JUnit format can't provide. Below is a sample of the output of such a reporter:
</p>
<pre class="brush: xml">
<testng-results>
<suite name="Suite1">
<groups>
<group name="group1">
<method signature="com.test.TestOne.test2()" name="test2" class="com.test.TestOne"/>
<method signature="com.test.TestOne.test1()" name="test1" class="com.test.TestOne"/>
</group>
<group name="group2">
<method signature="com.test.TestOne.test2()" name="test2" class="com.test.TestOne"/>
</group>
</groups>
<test name="test1">
<class name="com.test.TestOne">
<test-method status="FAIL" signature="test1()" name="test1" duration-ms="0"
started-at="2007-05-28T12:14:37Z" description="someDescription2"
finished-at="2007-05-28T12:14:37Z">
<exception class="java.lang.AssertionError">
<short-stacktrace>
<![CDATA[
java.lang.AssertionError
... Removed 22 stack frames
]]>
</short-stacktrace>
</exception>
</test-method>
<test-method status="PASS" signature="test2()" name="test2" duration-ms="0"
started-at="2007-05-28T12:14:37Z" description="someDescription1"
finished-at="2007-05-28T12:14:37Z">
</test-method>
<test-method status="PASS" signature="setUp()" name="setUp" is-config="true" duration-ms="15"
started-at="2007-05-28T12:14:37Z" finished-at="2007-05-28T12:14:37Z">
</test-method>
</class>
</test>
</suite>
</testng-results>
</pre>
<p>This reporter is injected along with the other default listeners so you can get this type of output by default. The listener provides some properties that can tweak the reporter to fit your needs. The following table contains a list of these properties with a short explanation:
</p>
<table border="1" width="100%" id="table6">
<tr>
<th>Property</th>
<th>Comment</th>
<th>Default value</th>
</tr>
<tr>
<td>outputDirectory</td>
<td>A <tt>String</tt> indicating the directory where should the XML files be outputed.</td>
<td>The TestNG output directory</td>
</tr>
<tr>
<td>timestampFormat</td>
<td>Specifies the format of date fields that are generated by this reporter</td>
<td>yyyy-MM-dd'T'HH:mm:ss'Z'</td>
</tr>
<tr>
<td>fileFragmentationLevel</td>
<td>An integer having the values 1, 2 or 3, indicating the way that the XML files are generated:
<br>
<pre>
1 - will generate all the results in one file.
2 - each suite is generated in a separate XML file that is linked to the main file.
3 - same as 2 plus separate files for test-cases that are referenced from the suite files.
</pre>
</td>
<td>1</td>
</tr>
<tr>
<td>splitClassAndPackageNames</td>
<td>This boolean specifies the way that class names are generated for the <tt><class></tt> element.
For example, you will get <tt><class class="com.test.MyTest"></tt> for false and <tt><class class="MyTest" package="com.test"></tt> for true.
</td>
<td>false</td>
</tr>
<tr>
<td>generateGroupsAttribute</td>
<td>A boolean indicating if a <tt>groups</tt> attribute should be generated for the <tt><test-method></tt> element. This feature aims at providing a
straight-forward method of retrieving the groups that include a test method without having to surf through the <tt><group></tt> elements.
</td>
<td>false</td>
</tr>
<tr>
<td>generateTestResultAttributes</td>
<td>
A boolean indicating if an <tt><attributes></tt> tag should be generated for each <tt><test-method></tt> element, containing the test result
attributes (See <tt>ITestResult.setAttribute()</tt> about setting test result attributes). Each attribute <tt>toString()</tt> representation will be
written in a <tt><attribute name="[attribute name]"></tt> tag.
</td>
<td>false</td>
</tr>
<tr>
<td>stackTraceOutputMethod</td>
<td>Specifies the type of stack trace that is to be generated for exceptions and has the following values:
<br>
<pre>
0 - no stacktrace (just Exception class and message).
1 - a short version of the stack trace keeping just a few lines from the top
2 - the complete stacktrace with all the inner exceptions
3 - both short and long stacktrace
</pre>
</td>
<td>2</td>
</tr>
<tr>
<td>generateDependsOnMethods</td>
<td>Use this attribute to enable/disable the generation of a <tt>depends-on-methods</tt> attribute for the <tt><test-method></tt> element.
</td>
<td>true</td>
</tr>
<tr>
<td>generateDependsOnGroups</td>
<td>Enable/disable the generation of a <tt>depends-on-groups</tt> attribute for the <tt><test-method></tt> element.
</td>
<td>true</td>
</tr>
</table>
<p>
In order to configure this reporter you can use the <tt>-reporter</tt> option in the command line or the <a href="http://testng.org/doc/ant.html">Ant</a>
task with the nested <tt><reporter></tt> element. For each of these you must specify the class <tt>org.testng.reporters.XMLReporter</tt>.
Please note that you cannot configure the built-in reporter because this one will only use default settings. If you need just the XML report with custom settings
you will have to add it manually with one of the two methods and disable the default listeners.
</p>
<!------------------------------------
YAML
------------------------------------>
<h3><a class="section" name="yaml">YAML</a></h3>
TestNG supports <a href="http://www.yaml.org/">YAML</a> as an alternate way of specifying your suite file. For example, the following XML file:
<pre class="brush: xml">
<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" >
<suite name="SingleSuite" verbose="2" thread-count="4" >
<parameter name="n" value="42" />
<test name="Regression2">
<groups>
<run>
<exclude name="broken" />
</run>
</groups>
<classes>
<class name="test.listeners.ResultEndMillisTest" />
</classes>
</test>
</suite>
</pre>
<p>and here is its YAML version:</p>
<pre class="brush: plain">
name: SingleSuite
threadCount: 4
parameters: { n: 42 }
tests:
- name: Regression2
parameters: { count: 10 }
excludedGroups: [ broken ]
classes:
- test.listeners.ResultEndMillisTest
</pre>
Here is <a href="https://github.com/cbeust/testng/blob/master/src/test/resources/testng.xml">TestNG's own suite file</a>, and its <a href="https://github.com/cbeust/testng/blob/master/src/test/resources/testng.yaml">YAML counterpart</a>.
<p>
You might find the YAML file format easier to read and to maintain. YAML files are also recognized by the TestNG Eclipse plug-in. You can find more information about YAML and TestNG in this <a href="http://beust.com/weblog/2010/08/15/yaml-the-forgotten-victim-of-the-format-wars/">blog post</a>.
<!---------------------------------------------------------------------->
<a name="testng-dtd">
<hr width="100%">
<p>Back to my <a href="http://beust.com/weblog">home page</a>.</p><p>Or check out some of my other projects:</p><ul>
<li><a href="http://beust.com/ejbgen">EJBGen</a>: an EJB tag
generator.</li><li><a href="http://testng.org">TestNG</a>: A testing framework using annotations, test groups and method parameters. </li><li><a href="http://beust.com/doclipse">Doclipse</a>: a JavaDoc tag
Eclipse plug-in.</li><li><a href="http://beust.com/j15">J15</a>: an Eclipse plug-in to help
you migrate your code to the new JDK 1.5 constructs.</li><li><a href="http://beust.com/sgen">SGen</a>: a replacement for
XDoclet with an easy plug-in architecture.</li><li><a href="http://beust.com/canvas">Canvas</a>: a template generator
based on the Groovy language.</li></ul><p>
</p>
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-238215-2";
urchinTracker();
</script>
|