0% found this document useful (0 votes)
16 views29 pages

Test NG Setup

TestNG is an automation framework used for various testing types, featuring annotations like BeforeClass, AfterClass, and Data Provider for flexible test management. The document outlines the setup requirements, installation steps for Maven, Perforce, and Code Collaborator, as well as configuration and execution of TestNG test cases in Eclipse. It also covers creating tests, using assertions, and sending test cases for review, emphasizing best practices and logging methods.

Uploaded by

miranipriyanka93
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views29 pages

Test NG Setup

TestNG is an automation framework used for various testing types, featuring annotations like BeforeClass, AfterClass, and Data Provider for flexible test management. The document outlines the setup requirements, installation steps for Maven, Perforce, and Code Collaborator, as well as configuration and execution of TestNG test cases in Eclipse. It also covers creating tests, using assertions, and sending test cases for review, emphasizing best practices and logging methods.

Uploaded by

miranipriyanka93
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 29

TestNG Overview.

TestNG is an automation framework which is used for different range of testing.


E.g. Unit Testing, Functional Testing etc.
TestNG is popular because of its Annotations.
It contains the following annotations:
1. GUID: Guid Stands for Globally Unique Identifier. This Guid is unique for every test case.
2. Before Class: Functions which get executed before the class execution.
3. After Class: Functions which get executed after the class execution.
4. Before Method: Gets executed before every test case.
5. After Method: Gets executed after every test case.
6. Data Provider: Using data provider you can run single Test case for different input.
TestNG has a concept of test groups.
This allows you to tag tests across classes with the same group name. You can then run all
tests that belong to that group no matter what class they belong to. We are currently only using
this to mark test categories: Sanity, Confidence, or Full.

TestNG Environment Setup


Requirements:
1. Apache-Maven-3.2.1
2. Perforce
3. Code Collaborator
4. Eclipse
5. GUIDGen.exe
You can find all this applications to Following Location
TestNg Software (file://CLM-PUN-002332/TestNg%20Software)
Installing and Setup Maven

Install Maven 2.2.1 from http://maven.apache.org/download.html or you can get it from shared
Directory
After the above step add following Environment Variable (in my case)
- Variable Name: M2_Home
Variable Value: C:\TestNg\apache-maven-3.2.1\apache-maven-3.2.1

- Variable Name: M2
Variable Value: C:\TestNg\apache-maven-3.2.1\apache-maven-3.2.1\bin

- Add %M2% to your Class path variable.

Installation Of Perforce

Step1: Click on the Perforce.exe select server and click Next.


Step2: Enter the Port Number 11842

Step 3: Enter the server name pun-rnd-scm01.bmc.com and Click Next.


Step 4: Click Finish

Step 5: After that Go to installation Directory of perforce in my case “C:\Program Files (x86)\
Perforce” click on P4v.exe
Enter the server name “pun-rnd-scm01.bmc.com:11842”
Enter your Username, Workspace Name and Click on OK.

Step 6: After that you will get the screen.


Step 7: Create New Workspace
Go to the location //remedy_qa/ars_qa/main/TestNG from Depot and right click and select get
revision. This will add the TestNG source code to your local workspace.

Installation of Code Collaborator


Step 1: Execute the Code Collaborator App.
Step 2: Click next till you get the Following Window. Enter the server URL
” http://p4collab:8484/” .your Username and password and Click Next
Step 3: In next window just click next and it will install the Code Collab.

Step 4: On your system tray right click on code collaborator icon and select open GUI.
Step 5: You will get the Window as below, Click on add.

Step 6: Enter the P4 executable (Path up to the installation directory of perforce)


Enter the P4 Port pun-rnd-scm01.bmc.com:11842, enter your adprod username, password
and P4client. And click validates, and then Ok.
Step 7: Next Go to File ->Preference.
Enter server URL http://p4collab:8484/. And you username and click on test Connection.
It should get successful without any error, then Click OK.
After that it will automatically get synced with your perforce. (Close perforce and reopen it you
can see the smart bear name in Tool> Smart Bear add to review.)
Build the Code
Once you get the code from Perforce you need to build the code.
 Open a command prompt at the TestNG Source Code directory. Execute the Batch file
compile.bat
 It will load all the Dependent jar files which are required for the Execution of TestNG
Test case.
 From the same command prompt, run mvn eclipse: eclipse to generate the Eclipse
projects.
Eclipse Configuration

Install and configure TestNG's Eclipse plug-in on Eclipse

1. Click on the Help -> Install New Software menu


2. Click on the Available Software tab
3. Click on the Add Site button
4. Enter http://beust.com/eclipse as the Location and click the OK button
5. Check the box next to TestNG
6. Click the Install button and follow the wizard to install the plug-in
7. Let the wizard restart Eclipse

Import the TestNG Project into Eclipse

1. Click on the File -> Import menu


2. Choose General -> Existing Projects into Workspace
3. Click on the Next button
4. Click on the Browse button next to "Select root directory:" and choose the directory
where you checked out TestNG from source control
5. Click on the Finish button to import the project
6. Configure Eclipse to have the M2_REPO environment variable

1. Go back to Eclipse and click on the Window -> Preferences menu


2. Click on Java -> Build Path -> Class path Variables on the list
3. Add the M2_REPO variable pointing to your local Maven repository (In my case
C:\Users\<user>\.m2\repository.)

Running Testcase

1. Create and Configure run.config

 In the Package Explorer find the file named run.config. Sample


 Right-click on run.config. Sample and choose Copy from the menu
 Right-click on the TestNG project and choose Paste from the menu
 When the Name Conflict dialog pops up, rename the file to "run.config" and click the
OK button
 Double-click on the run.config file.
 Click on the Source tab at the bottom of the editor view to see the raw XML instead
of Eclipse’s XML builder.
 Edit the run.config file according to the comments in the file.
 Specify the Server name, username , password, port
2. Running Testcase in class.

 Open Eclipse and launch your workspace that contains TestNG


 Open the file that contains the tests you want to run.
 From the Run menu or the Run button on the toolbar, choose Run As -> TestNG
Test.
 If you want to run the single Testcase then select test case and right click and select
run as TestNg
 Once test case gets executed you can check whether test case get passed or failed
on eclipse console

Create a Test case.


This is the simple Testcase

@Test (groups=Groups.CATEGORY_CONF, description="")


@GUID ("551e0bfa-d34f-4afd-b4b7-462b951016ba")
@Bug ("")
public void sampleTestcase () {
String submitterValue="Demo";
String shortDescription="Bmc Software";

Entry entry =new Entry ();


Try {
entry.Put (Constants.AR_CORE_SUBMITTER, new Value(submitterValue));
entry.Put (Constants.AR_CORE_SHORT_DESCRIPTION, new Value(shortDescription));
primaryServer.createEntry (sBaseForm1, entry);
} catch (AR Exception e){
Assert. Fail ("Failed to Create Entry on the form",e);
}
}
Creating Tests

AR System Java API

Because the TestNG framework is Java, this means that we are using the AR System Java API
to make calls to the AR Server. You can get a copy of the Javadoc’s by looking in the AR
Server's install directory, specifically the arserver\api\lib directory. In that directory there will be
a file named ardocXX.jar where XX is the version number. You can extract that file and open the
index.html file in a web browser. All API calls are made from the ARServerUser class. In the C#
automation framework we use the .NET API and the BMC.ARSystem.Server class to make all
API calls. The ARServerUser class serves the same purpose but you'll notice that the names of
the API calls may not be the same.

Naming Conventions

The following table shows the base package name for each product.

Product Package
API com.bmc.arsys.tests.api
AR Import com.bmc.arsys.tests.arimport
AR Server com.bmc.arsys.tests.server
Email Engine com.bmc.arsys.tests.emailengine
Web Services com.bmc.arsys.tests.webservices

Test class names should also reflect what you are testing. If you are writing server tests for
merge entry then put them in the com.bmc.arsys.tests.server.entries package with a class name
like MergeEntry. The names of the methods should be descriptive as well. The test method's
name should be a short version of the test description. For example, if you're verifying that the
status history field is updated during merge entry, a good function name would be
verifyStatusHistoryUpdated.

Configuration methods
Just like C# and JUnit frameworks, you can perform some setup and teardown work during a
test. There are four types of configuration methods we will use in TestNG, denoted by the
annotation used. Note: You must include the alwaysRun = true on the configuration methods or
else they may not be run because we run with groups from the automation controllers.

@BeforeClass(alwaysRun = true)
public void classSetup()
{
// gets called before any test in this class is run
// often used for importing definitions, data,
// or changing a server info setting
}

@AfterClass(alwaysRun = true)
public void classTeardown()
{
// gets called after every test in this class is run
// often used for cleaning up test data and
// resetting a server info setting
}

@BeforeMethod(alwaysRun = true)
public void beforeMethod()
{
// gets called before every individual test
// often used for resetting test data
}

@AfterMethod(alwaysRun = true)
public void afterMethod()
{
// gets called after every individual test
// often used for resetting test data
}
Assertions
The point of an automated test case is to perform some action and verify that the expected
behavior happened. For example, a workflow test may create an entry to trigger a set field
action. You need to verify that the value put in that field is correct and TestNG provides a class
to do that: Assert. This class, similar to the class in JUnit, provides many static methods to
compare two values. See http://testng.org/javadocs/org/testng/Assert.html for more information.
There are some types of comparisons that TestNG's Assert class does not handle that are
specific to AR System and the Java API. There is a helper class named AssertHelper in our
framework that provides some helper methods for common tests, like verifying a correct error
code was thrown. Look at the class or use Eclipse's code completion pop-up to see what it
provides.

Tips and Tricks

Importing definition files


Unless you are writing import or export automation it is highly recommended that you not
directly import definition files to the server with the Java API. Like the C# framework, there is a
definition importer that will import a definition and a separate method to delete the objects that
were imported. If you have a definition file that all your tests need you can pass it to ServerTest
or BaseTest's constructor, depending on which base class you are extending. For example:

public MyServerTest()
{
super("MyDefinition.def");
}

If you need to import more than one definition then use the helper method in BaseTest named
importDefinition. If you do then make sure to observe the importTestData parameter in the
run.config file. For example:

@BeforeClass(alwaysRun = true)
public void import()
{
if(AutoConfig.getRunConfig().isImportTestData())
{
importDefinition(primaryServer, true, "File1.def", "File2.def", "File3.def");
}
}

In either case (the constructor or importDefinition method) the BaseTest class will keep track of
the definition files and during the @AfterClass method it will cleanup the objects that were
imported. The BaseTest class will only cleanup the objects if cleanupTestData in the run.config
file is set to true.

Logging
The TestNG framework uses log4j for logging. In the AutoLog class there is a method to retrieve
the logger, getLogger. Once you have that you can log messages depending on their level:
debug, info, warn, error, and fatal. Do not use System.err or System.out to print messages to
the console. Everything that is logged is not only logged to a file but also printed to the console.
Do not use printStackTrace after catching an exception. The log methods allow you to pass an
exception and the logger will print out the stack trace along with the message you provide, for
example:

try
{
...
}
catch(ARException e)
{
// don't fail test, just log and move on
AutoLog.getLogger().info("API call failed, just log and move on", e);
}

Negative tests
To write a proper negative test, where an API call should throw an exception, you need to
include two assertions: a failure if an exception was not thrown, and an assertion that the
correct error code was thrown. In order to help with the second part there is a helper method in
the AssertHelper class named assertErrorCode that will verify that the correct error code was
returned. For example, to verify that submitting a record without a value in the Short Description
field:

try
{
// create entry without a required field
Entry entry = new Entry();
entry.put(Constants.AR_CORE_SUBMITTER, new Value("Demo"));
primaryServer.createEntry("My Test Form", entry);
Assert.fail("API call should have thrown an exception");
}
catch(ARException e)
{
AssertHelper.assertErrorCode(e, ARErrors.AR_ERROR_REQ_VALUE_MISSING);
}

Creating test users and groups


If you need to create a new test user or group there are some helper methods in the TestHelper
class that can do some of the work for you. Then to connect to the AR Server as that new user
there is a helper method in AutoConfig. Suppose you need to create a new test user and group
for some permissions tests you would first create the group and then the user. Both of the
helper methods will return the request IDs of the entry that was created in the Group or User
form. This way you can use that request ID to delete the group or user when your test is done.
Here is an example on how to use the helper methods:

private String groupRequestId = null;


private String userRequestId = null;
private ARServerUser testUser = null;

@BeforeClass(alwaysRun = true)
public void setup throws ARException
{
// create group
groupRequestId = TestHelper.createGroup(primaryServer, "ExampleTestGroup", 5123,
Constants.AR_GROUP_TYPE_VIEW,
Constants.AR_GROUP_CATEGORY_REGULAR);

// create user and login


userRequestId = TestHelper.createUser(primaryServer, "ExampleTestUser",
"ExamplePassword", "ExampleTestGroup");
testUser = AutoConfig.connectToServer("ExampleTestUser", "ExamplePassword",
primaryServer);
}

@AfterClass(alwaysRun = true)
public void tearDown()
{
// logout user
testUser.logout();

// delete user
try
{
if(userRequestId != null)
primaryServer.deleteEntry("User", userRequestId, 0);
}
catch(ARException e)
{
String message = "Error while trying to delete user.";
AutoLog.getLogger().warn(message, e);
}

// delete group
try
{
if(groupRequestId!= null)
primaryServer.deleteEntry("Group", groupRequestId, 0);
}
catch(ARException e)
{
String message = "Error while trying to delete group.";
AutoLog.getLogger().warn(message, e);
}
}

How to send the test case for review.


Once you write the test case you need to review the Testcase that’s where code collaborator
come in to picture. By using code collaborator we send the code for review.

Step 1- From Perforce Select the class from which you need to send your test case for review
and right click and select checkout.
Step 2: once you will checkout. You will get one pending change list.
Step 3- right click on the the default change list and select the Edit the default changelist.write
the change list description means the description about your test case. And click on “Save as
Numbered Change list”
Step 4 – you will get the numbered change list. Select it and right click after that select
“SmartBear- Add to Review”
Step 5-it will open the following window click on finish.
Step 6- it will open the browser.enter your adprod credentials.
Step 7- you will get the Following window. You just need to select the reviewer.
Reviewer is the person in which you need to send your code for review.

Step 8- if reviewer will send you suggestion you have to modify the test case as per suggestions
and again send it for review .once reviewer will accept your test case, the last important step is
check in code into perforce
How to check in you testcase Into Perforce.

Step1- select the change list and right click and select “submit” option.it will submit your code.
Question And answers.

You might also like