Write Test Cases Using TestNG
Login & Logout TestCase
package asc; import org.testng.annotations.Test; public class loginTest { @Test(priority=1,description="This is the login Test") public void logintest() { System.out.println("Login is Sucessful"); } @Test(priority=2,description="This is the logout Test") public void logoutTest() { System.out.println("Logout is Sucessful"); } }
code is a simple example of how to use TestNG in a Java project. The code defines a class loginTest with two test methods: logintest and logoutTest. Here’s a breakdown of what each part of the code does:
Package Declaration
package asc;
This declares that the class loginTest is part of the package asc.
Importing TestNG Annotations
import org.testng.annotations.Test;
This imports the @Test annotation from the TestNG framework, which is used to denote test methods.
Class Definition
public class loginTest {
This defines a public class named loginTest.
Test Method logintest
@Test(priority=1, description="This is the login Test") public void logintest() { System.out.println("Login is Sucessful"); }
@Test(priority=1, description="This is the login Test") This annotation marks the 'logintest' method as a test method. The 'priority' attribute is set to '1', indicating that this test should run first. The description attribute provides a brief description of the test.
public void logintest() { ... } : This is the test method which, when executed, prints "Login is Successful" to the console.
Test Method logoutTest
@Test(priority=2, description="This is the logout Test") public void logoutTest() { System.out.println("Logout is Sucessful"); }
@Test(priority=2, description="This is the logout Test") : This annotation marks the logoutTest method as a test method. The priority attribute is set to 2, indicating that this test should run after the logintest method. The description attribute provides a brief description of the test.
public void logoutTest() { ... } : This is the test method which, when executed, prints "Logout is Successful" to the console.
Summary
@Test Annotation : Marks methods as test methods for TestNG.
priority Attribute : Determines the order in which the test methods are executed. Lower numbers are executed first.
description Attribute : Provides a human-readable description of the test method.
Test Methods : ' logintest ' and ' logoutTest ' are simple test methods that print messages to the console to simulate a login and logout process.
Execution
When you run this test class using TestNG, TestNG will execute the test methods in the order specified by their priority attributes. The output will be:
Login is Sucessful Logout is Sucessful
This basic structure can be expanded with more complex test logic, assertions, and setup/teardown methods as needed for more comprehensive testing.