TestNG Eclipse plug-in
Starting off with the installation of the TestNG plugin for Eclipse. TestNG is based on Java and you need to install it in case you haven’t already. Now to begin with the installation of TestNG plugin for Eclipse, you need to launch Eclipse IDE and then follow these steps. Step #1: Once Eclipse is launched, click on Help and then Eclipse.
The TestNG Eclipse plug-in allows you to run your TestNG tests from Eclipse and easily monitor their execution and their output. It has its own project repository called testng-eclipse.
Table of Contents
- Create a small project that will allow us to reproduce this bug. In most cases, one or two Java source files and a testng.xml file should be sufficient. Then you can either zip it and email it to the testng-dev mailing-list or make it available on an open source hosting site, such as github and email testng-dev so we know about it. Please make sure that this project is self contained so that we can build it right away (remove the dependencies on external or proprietary frameworks, etc.).
- 301 Moved Permanently.
- TestNG is freely available and can be easily installed in the Eclipse IDE using Eclipse Market. Upon installation, TestNG would be available as a library within the Eclipse environment. Create a new Java Project and configure the build path using a TestNG library.
- No necessarily to do all those import export and stuff. Eclipse has a problem with itself. But it can be solved with a simple startup of. Cmd eclipse.exe -clean. It will take awhile to startup but eventually it worked for me. Coz partially sometimes eclipse has problem unzipping files or so n partial files unzipped.
2 - Creating a TestNG class
3 - Launch configurations
3.1 - From a class file
3.2 - From groups
3.3 - From an XML file
3.4 - From a method
3.5 - Specifying listeners and other settings
4 - Viewing the results
5 - Search
6 - The Summary tab
7 - Converting JUnit tests
8 - Quick fixes
9 - Preferences and Properties
9.1 - Workbench Preferences
9.2 - Project Properties
10 - M2E Integration
1 - Installation
Follow the instructions to install the plug-in.
NOTE: since TestNG Eclipse Plugin 6.9.10, there is a new optional plug-in for M2E (Maven Eclipse Plugin) integration. It's recommended to install it if your Java project(s) are managed by Maven.Once done, restart Eclipse and select the menu Window / Show View / Other.. and you should see the TestNG view listed in the Java category.
NOTE: since TestNG Eclipse Plugin 6.9.8, the minimum required TestNG version is 6.5.1
2 - Creating a TestNG class
To create a new TestNG class, select the menu File / New / TestNG:If you currently have a Java file open in the editor or if you have a Java file selected in the Navigator, the first page of the wizard will show you a list of all the public methods of that class and it will give you the option to select the ones you want to test. Each method you select on this page will be included in the new TestNG class with a default implementation that throws an exception, so you remember to implement it.The next page lets you specify where that file will be created, whether it should contain default implementation for some configuration methods, if you'd like a data provider and finally, if a testng.xml file should be generated.The plug-in will make a guess about the best location where this file should be created (for example, if you are using Maven, the default location will be under src/test/java).
3 - Launch configuration
Once you have created classes that contain TestNG annotations and/or one or more testng.xml files, you can create a TestNG Launch Configuration. Select the Run / Run.. (or Run / Debug..) menu and create a new TestNG configuration:
You should change the name of this configuration and pick a project, which can be selected by clicking on the Browse.. button at the top of the window.
- Log Level: specify the value (0-10) for different verbose log levels
- Verbose: enable the runtime TestNG verbose log
- Debug: enable more runtime TestNG debug info
- Serialization Protocol: the serialization protocol used for communicating between TestNG Eclipse Plugin and TestNG runtime.
- Json Serialization: This protocol was introduced in the TestNG Eclipse plug-in 6.9.11 to better communicate with a different JRE running TestNG.
- Object Serialization: This protocol packs the message data with Java serialization.
- String Serialization: Deprecated.
Then you choose to launch your TestNG tests in the following ways:
4.2.1 - From a class file
Make sure the box near Class is checked and then pick a class from your project. You can click on the Browse.. button and pick it directly from a list. This list only contains classes that contain TestNG annotations:
3.2 - From groups
Pixelmator pro photoshop. If you only want to launch one or several groups, you can type them in the text field or pick them from a list by clicking on the Browse.. button
3.3 - From a definition fileFinally, you can select a suite definition from your project. It doesn't have to be namedtestng.xml, the plug-in will automatically identify all the applicable TestNG XML files in your project:You can type the regex on the filter text field to narrow down to suite definition files matching your search from a long list of files.
3.4 - From a method
This launch isn't accomplished from the Launch dialog but directly from your Outline view:You can right-click on any test methods and select Run as.. / TestNG test and only the selectedmethod will be run (not shown on the above screenshot because I couldn't find a way to capturea contextual menu).
Method launching is also available from the Package Explorer view and from the Java Browser perspective.
Once you have selected one of these launches, you can also choose the logging of level. Then you can launch the tests by pressing the Debug (or Run) button, which will switch you to the Debug perspective and will open the main TestNG view.
3.5 -Specifying listeners and other settingsAs you saw above, the plug-in will let you start tests in many different ways: from an XML file, from a method, a class, etc.. When you are running an XML file, you can specify all the settings you want for this run in the XML file, but what if you want to run a package in parallel mode with specific listeners? How can you configure the settings for all the launches that are not done from an XML file?
In order to give you access to the most flexibility, TestNG lets you specify an XML suite file for all these launches, which you can find in the Preferences menu:If you specify a valid suite file as 'XML template file', TestNG will reuse all the settings found in this XML file, such as parallel, name, listeners, thread pool size, etc.. Only the <test> tags in this file will be ignored since the plug-in will replace these by a generated <test> tag that represents the launch you chose.
4 - Viewing the test results
The above view shows a successful run of the tests: the bar is green and no failed tests are reported. The All tests tab shows you a list of all the classes and methods that were run.
If your test run contains failures, the view will look like this:
You can use the Failed tests tab to display only these tests that failed, and when you select such a test, the stack trace will be shown on the right-hand pane. You can double click on the offending line to be taken directly to the failure in your code.
5 - Search
When you have hundreds of tests running, finding a specific one is not always easy, so you can type a few letters of the test method or its parameters in the Search box and the content of the tree will automatically narrow down to methods matching your search. Note in the screen shot above that the search also works on parameters provided by @DataProvider.
6 - Summary
The Summary tab gives you statistics on your test run, such as the timings, the test names, the number of methods and classes, etc… Since the results are shown in a table, you can also sort on any criterion you like for easier parsing. This is especially handy when you are trying to determine what tests take the longest time.
The search box works in this view as well, and note that in the screen shot below, the Time column is sorted in decreasing order:
7 - Converting JUnit testsYou can easily convert JUnit 3 and JUnit 4 tests to TestNG.
Your first option is to use the Quick Fix function:
Convert from JUnit 3
You can also convert packages or entire source folders with the conversion refactoring:The refactoring wizard contains several pages: This page lets you generate a testng.xml automatically. You can configure whether to include your test classes individually or by package, the suite and test name and also whether these tests should run in parallel.This page gives you an overview of the changes that are about to be performed. You can also decide to exclude certain files from the refactoring.
Convert from JUnit 4
When you are done, press the 'Finish' button. Like all Eclipse refactorings, you can undo all these changes in one click:
8 - Quick fixesThe TestNG Eclipse plug-in offers several quick fixes while you are editing a TestNG class (accessible with Ctrl-1 on Windows/Linux and ⌘-1 on Mac OS):
Convert to JUnitThis was covered in the previous section.
Pushing and pulling @Test annotationsIf you have several test methods annotated with @Test and you'd like to replace them all with a single @Test annotation at the class level, choose the 'Pull annotation' quick fix. Reciprocally, you can move a class level @Test annotation onto all your public methods or apply a quick fix on an assert method to automatically import it.
9 - Preferences and Properties
9.1 - Workbench PreferencesTestNG workbench preferences:
The preferences here are shared among projects and launch configurations.
- Output directory: the path where to store the output including temp files, report files, etc.. By default, the path is relative to each project except if you check the option Absolute output path below.
- Absolute output path: whether the path above is absolute or relative to the current project.
- Disable default listeners: disable the default listeners when launching TestNG.
- Show view when test complete: activate the TestNG result view when the test completes.
- Template XML file: the absolute path of the template XML file used to genernate the custom test suite XML file before launching.
- Excluded stack traces:
- Predefined Listeners:
9.2 - Project PropertiesProject level properties:
Here are properties on each project level, it will override the same properties if defined in TestNG workbench preferences
- Output directory: for example, in the figure above, I prefer to put the output to maven 'target' directory rather than the default one under project root
- Watch testng-result.xml:
- Template XML file: see in TestNG workbench preferences
- Predefined Listeners: see in TestNG workbench preferences
10 - M2E IntegrationThe (optional) TestNG M2E Integration plug-in was introduced in 6.9.10.It allows you to run your tests with System Properties or JVM settings, which are defined by maven-surefire-plugin or maven-failsafe-plugin of pom.xml, to be appended to the runtime TestNG process.Once this plugin installed, you can see a dedicated preference page (workspace level settings):or on the project properties page. You can override workspace settings with project specific ones:Let's say there is maven-surefire-plugin confguration in your pom.xml:You can pass the following Maven configuration parameters to the TestNG process:
- argLine: the JVM arguments
- systemPropertyVariables: the system properties
- environmentVariables: the environment variables
NOTE: as the snippet of the configuration above shows, properties placeholders (e.g.
systemPropertyVariables will be substituted and correctly passed to the TestNG process as long as the properties are visible on the Maven project (e.g. defined in the current
pom.xml, or inherited from the parent
NOTE: if your maven-surefire-plugin is defined in a Maven profile, you will need to select the Maven profile which contains the maven-surefire-plugin configuration: 'Right click on the project -> Maven -> Select Maven Profiles..', then check the profile you need.
In this article, we'll introduce the TestNG testing framework.
We'll focus on: framework setup, writing simple test case and configuration, test execution, test reports generation, and concurrent test execution.
Let's start by adding the Maven dependency in our pom.xml file:
The latest version can be found in the Maven repository.
When using Eclipse, the TestNG plugin may be downloaded and installed from the Eclipse Marketplace.
3. Writing a Test Case
To write a test using TestNG, we just need to annotate the test method with org.testng.annotations.Test annotation:
4. Test Configurations
While writing test cases, often we need to execute some configuration or initialization instructions before test executions, and also some cleanup after completion of tests. TestNG provides a number of initialization and clean-up features at method, class, group and suite levels:
The setup() method annotated with @BeforeClass annotations will be invoked before execution of any methods of that test class, and tearDown() after execution all methods of the test class.
Similarly, we can use the @BeforeMethod, @AfterMethod, @Before/AfterGroup, @Before/AfterTest and @Before/AfterSuite annotations for any configuration at method, group, test and suite levels.
5. Test Execution
We can run the test cases with Maven's “test” command, it will execute all the test cases annotated with @Test putting them to a default test suite. We can also run test cases from the TestNG test suite XML files, by using the maven-surefire-plugin:
Note that if we have multiple XML files, covering all test cases, we can add all of them in the suiteXmlFiles tag:
In order to run the test standalone, we need to have the TestNG library in the classpath and the compiled test class along with the XML configuration file:
6. Grouping Tests
Tests can be run in groups, for example out of 50 test cases 15 can be grouped together and executed leaving others as is.
In TestNG grouping tests in suites are done using XML file:
Notice that, both the test classes RegistrationTest, SignInTest now belongs to the same suite and once suite is executed, test cases in this class will get executed.
Apart from test suites, we can also create test groups in TestNG, where instead of test classes methods are grouped together. In order to do that, add the groups parameter in the @Test annotation:
Let’s use an XML to execute the groups:
This will execute the test method tagged with group regression, in the SummationServiceTest class.
7. Parameterized Tests
Parameterized unit tests are used for testing the same code under several conditions. With the help of parameterized unit tests, we can set up a test method that obtains data from some data source. The main idea is to make the unit test method reusable and to test with a different set of inputs.
In TestNG, we can parametrize tests using @Parameter or @DataProvider annotation. While using the XML file annotate the test method with @Parameter:
Using data from XML file is useful, but we often need more complex data. @DataProvider annotation is used to handle these scenarios, which can be used to map complex parameter types for testing methods.@DataProvider for primitive data types:
Using this, any object that has to be tested can be created and used in the test. This is mostly useful for integration test cases.
8. Ignoring Test Cases
We sometimes want not to execute a certain test case, temporarily during the development process. This can be done adding enabled=false, in the @Test annotation:
9. Dependent Tests
Let's consider a scenario, where if the initial test case fails, all subsequent test cases should be executed, and rather be marked as skipped. TestNG provides this feature with the dependsOnMethods parameter of the @Test annotation:
Notice that, the login test case depends on the email validation test case. Thus, if email validation fails the login test will be skipped.
10. Concurrent Test Execution
TestNG allows tests to run in parallel or in multi-threaded mode, thus providing a way to test these multi-threaded pieces of code.
You can configure, for methods, classes, and suites to run in their own threads reducing the total execution time.
10.1. Classes and Methods in Parallel
To run test classes in parallel, mention the parallel attribute in the suite tag in XML configuration file, with value classes:
Note that, if we have multiple test tags in the XML file, these tests can also be run in parallel, by mentioning parallel =” tests”. Also to execute individual methods in parallel, mention parallel =” methods”.
10.2. Multi-Threaded Execution of Test Method
Let's say we need to test the behavior of a code when running in multiple threads. TestNG allows to run a test method in multiple threads:
The threadPoolSize indicates that the method will run in n number of threads as mentioned. The invocationCount and timeOut indicate that the test will be executed multiple times and fail the test if it takes more time.
11. Functional Testing
TestNG comes with features which can be used for functional testing as well. In conjunction with Selenium, it can either be used to test functionalities of a web application or used for testing web services with HttpClient.
Testng Eclipse Marketplace
More details about functional testing with Selenium and TestNG is available here. Also some more pieces of stuff on integration testing in this article.
Testng Eclipse Specify Environment
In this article, we had a quick look at how to setup TestNG and execute a simple test case, generate reports, concurrent execution of test cases and also a little about functional programming. For more features like dependent tests, ignoring test cases, test groups and suites you can refer our JUnit vs TestNG article here.
The implementation of all the code snippets can be found over on Github.