Unit 6: Test Automation and Management/Reporting Frameworks
I. Orientation — Build Management and Test Execution
Maven and TestNG are complementary tools in automated testing. Maven manages a Java project’s build lifecycle, dependencies, plugins, and reports, while TestNG defines, organizes, executes, and reports automated test cases. Together with Selenium, they support repeatable, maintainable, and team-friendly test automation.
- Build management: Maven standardizes compiling, testing, packaging, and dependency retrieval through
pom.xml. - Test organization: TestNG groups test methods into suites, supports priorities and dependencies, and provides lifecycle annotations.
- Automation principle: A test should be repeatable, independently understandable, and executable from a command line or continuous integration server.
- Reporting convention: Test results should identify passed, failed, skipped, duration, and failure details.
- Project convention: Java source code normally belongs in
src/main/java, while automated tests belong insrc/test/java.
II. Maven — Project and Build Management
Maven is an open-source build automation and project management tool primarily used for Java applications. Its central configuration file, pom.xml, describes the project and controls its build lifecycle.
A. Introduction to Maven
Maven uses convention over configuration: standard directory structures and lifecycle phases reduce the amount of project-specific setup.
- Meaning: Maven comes from the Yiddish word for “accumulator of knowledge”; technically, it coordinates project construction and management.
- Core file:
pom.xmlmeans Project Object Model and contains coordinates, dependencies, plugins, and build settings. - Execution model: Commands such as
mvn testinvoke a predefined sequence of lifecycle phases. - Typical structure:
src/main/javastores application code;src/test/javastores test code;targetstores generated output.
B. Benefits and Features of Maven
Maven improves consistency by giving projects a standard build process and automatically resolving external libraries.
- Dependency management: Declaring
org.seleniumhq.selenium:selenium-java:4.xallows Maven to download Selenium and its transitive dependencies. - Repeatability: The same command, such as
mvn clean test, can run locally or in Jenkins with equivalent lifecycle behavior. - Convention: Standard paths allow tools and developers to locate source, test, and generated files predictably.
- Extensibility: Plugins add functions such as compilation, packaging, code analysis, and report generation.
- Repository support: Maven retrieves artifacts from local repositories, remote repositories, and Maven Central.
C. Activities Managed by Maven
Maven manages the activities required to move source code from development to a tested deliverable.
- Compilation: The compiler plugin converts
.javafiles into.classfiles. - Testing: The Surefire plugin runs unit and TestNG tests during the
testphase. - Packaging:
packagecreates an artifact such as a.jarfile. - Installation:
installplaces the artifact in the developer’s local Maven repository. - Deployment:
deploytransfers the artifact to a remote repository for team or release use. - Cleaning:
cleanremoves thetargetdirectory and previous build output.
D. Architecture of Maven
Maven architecture combines a project object model, lifecycle, plugins, and repositories.
- POM: Defines project identity and build instructions.
- Lifecycle: Standard phases include
validate,compile,test,package,verify,install, anddeploy. - Plugins: Maven delegates work to goals, such as
compiler:compileorsurefire:test. - Repositories:
- Local repository: Usually
~/.m2/repository; caches downloaded artifacts. - Remote repository: Stores artifacts on a server.
- Central repository: Public default source for many Java libraries.
- Local repository: Usually
- Coordinates:
groupId,artifactId, andversionuniquely identify a Maven artifact.
E. Installing/Configuring Maven
Maven requires a compatible Java Development Kit and environment configuration.
- Prerequisite: Install a JDK and verify it with
java -version. - Installation: Download Maven, extract it, and add its
bindirectory toPATH. - Verification: Run
mvn -version; the output should show Maven, Java, and operating-system details. - Configuration: Maven settings may be placed in
~/.m2/settings.xmlfor mirrors, proxies, credentials, or repository rules. - Build condition:
JAVA_HOMEshould point to the JDK installation rather than only a Java runtime.
F. Creating Maven Project
A Maven project can be generated from an archetype or created using an IDE.
- Coordinates: For example,
com.exampleis thegroupId,selenium-testsis theartifactId, and1.0-SNAPSHOTis the version. - Standard folders: Create
src/main/javaandsrc/test/java; Maven recognizes them without additional path configuration. - Command example:
BASHmvn archetype:generate - Build output: Compiled classes, reports, and packaged files are written under
target. - Naming rule: Test classes commonly use names such as
LoginTest.java, which makes their purpose clear and supports test discovery.
G. Understanding POM.xml File
The POM is an XML document that describes how Maven identifies and builds a project.
- Project identity: The combination below identifies the artifact:
XML<groupId>com.example</groupId> <artifactId>selenium-tests</artifactId> <version>1.0-SNAPSHOT</version> - Packaging:
<packaging>jar</packaging>selects JAR packaging;jaris the default. - Properties: A property such as
<maven.compiler.source>17</maven.compiler.source>centralizes the Java version. - Dependencies: The
<dependencies>section lists libraries required by application or test code. - Build section:
<build>configures plugins, test execution, source directories, and reporting behavior.
H. Adding Dependencies to POM.xml
Dependencies should be declared with a scope that matches how the library is used.
- Test scope: TestNG is normally available only while compiling and running tests:
XML<dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>7.10.2</version> <scope>test</scope> </dependency> - Selenium dependency:
selenium-javasupplies Selenium WebDriver APIs and related modules. - Version control: Explicit versions prevent accidental changes caused by repository updates.
- Transitive dependencies: Maven automatically downloads libraries required by a declared dependency.
- Conflict handling:
mvn dependency:treedisplays resolved dependencies and helps locate incompatible versions.
III. TestNG — Test Execution and Reporting Framework
TestNG is a Java testing framework inspired by JUnit but designed to support broader test configuration, grouping, parallel execution, and reporting. It is especially useful for Selenium suites containing many related browser tests.
A. Introduction to TestNG
TestNG uses annotations and an XML suite model to define how test methods are prepared and executed.
- Test marker:
@Testidentifies an executable test method. - Lifecycle:
@BeforeMethodruns before each test method;@AfterMethodruns afterward. - Suite model:
testng.xmlcan select classes, packages, groups, and execution settings. - Execution result: TestNG records passed, failed, and skipped methods with timing and exception information.
B. TestNG vs JUnit
Both frameworks execute Java tests, but TestNG provides features that are convenient for larger integration and Selenium suites.
- Annotations:
- TestNG: Uses
@BeforeSuite,@BeforeTest,@BeforeClass, and@BeforeMethod. - JUnit: Uses lifecycle annotations such as
@BeforeAll,@BeforeEach, and framework-specific equivalents.
- TestNG: Uses
- Organization: TestNG directly supports groups, priorities, dependencies, data providers, and suite XML configuration.
- Assertions: Both support assertions, although the exact assertion classes and lifecycle APIs differ by version.
- Parallelism: TestNG can configure parallel suites, tests, classes, or methods through
testng.xml. - Selection: Groups such as
smokeandregressionallow TestNG to run selected categories.
C. Downloading and Installing TestNG
TestNG can be installed through Maven or an IDE plugin.
- Maven method: Add the TestNG dependency to
pom.xml; Maven downloads it duringmvn test. - IDE method: Install the TestNG plugin from the IDE’s marketplace when direct IDE execution is required.
- Verification: A class importing
org.testng.annotations.Testconfirms that the library is available to the project. - Compatibility: Select a TestNG version compatible with the project’s Java version and build plugins.
- Recommended practice: Keep TestNG in the build file so every developer and CI server receives the same version.
D. Creating Test Cases Using TestNG Annotations
A TestNG test case is a Java method marked with @Test, usually supported by setup and cleanup methods.
- Basic test:
JAVAimport org.testng.annotations.Test; public class LoginTest { @Test public void validLogin() { System.out.println("Login validated"); } } - Setup:
@BeforeMethodcan create a WebDriver before each test. - Cleanup:
@AfterMethodshould calldriver.quit()so browser processes do not remain active. - Isolation: Each test should establish its own required state rather than depending on execution order.
- Naming: A method name such as
invalidPasswordShowsErrorcommunicates the behavior under test.
E. Creating Reports Using TestNG
TestNG creates execution information automatically and supports listener-based custom reporting.
- Default output: Results are commonly written to
test-output. - Recorded data: Reports include test names, status, duration, exceptions, and method-level details.
- Listeners: Implementations of
ITestListenercan react to events such as test success or failure. - Screenshots: Selenium listeners can capture a screenshot when a WebDriver test fails.
- Custom reports: ExtentReports or Allure can consume test events for richer dashboards, while TestNG remains the execution engine.
F. Understanding Annotations of TestNG
Annotations express test lifecycle, ordering, grouping, and data behavior.
- Lifecycle order: Suite setup may run before test setup, class setup, method setup, the test, method cleanup, class cleanup, and suite cleanup.
- Configuration:
@BeforeSuiteruns once before the suite;@BeforeClassruns before the first method in a class. - Grouping:
@Test(groups = "smoke")assigns a test to a named group. - Dependency:
@Test(dependsOnMethods = "createUser")prevents execution when the prerequisite fails. - Data:
@DataProvidersupplies multiple input sets to one test method.
G. Need of TestNG in Selenium
TestNG supplies structure that Selenium WebDriver alone does not provide.
- Browser lifecycle: Configuration annotations consistently start and close browsers around tests.
- Regression selection: Groups can run only
smoke,sanity, orregressiontests. - Parallel execution: Independent browser tests can run concurrently, reducing suite time.
- Failure handling: Failed methods and stack traces identify the WebDriver action or assertion that broke.
- Parameterized testing: A data provider can test multiple usernames and passwords without duplicating test code.
H. Running the Test
Tests can be launched from an IDE, Maven, or a TestNG suite file.
- Maven command:
BASHmvn clean test - IDE execution: Run a class, method, or
testng.xmlas a TestNG test. - Suite XML: The file can list test classes and define parallel mode or thread count.
- Exit status: Maven normally returns a nonzero status when tests fail, allowing CI to reject the build.
- Repeatability: Browser versions, drivers, environment URLs, and credentials should be controlled through configuration.
I. Checking Reports Created by TestNG
Generated reports should be inspected for both outcome and diagnostic detail.
- Main location: Open
test-output/index.htmlafter a TestNG run. - Status meaning: Passed indicates successful completion; failed indicates an exception or assertion failure; skipped indicates that execution was bypassed.
- Failure detail: Read the assertion message and stack trace to identify the expected and actual values.
- Timing: Long-running tests can expose slow page loads, waits, or environmental problems.
- Test hygiene: A report containing many skipped tests may indicate a failed dependency or incorrect suite selection.
J. Generating HTML Reports
HTML reporting converts raw test events into a navigable summary for developers and managers.
- Default report: TestNG’s HTML output includes suite and method summaries under
test-output. - Maven integration: The Surefire Report Plugin can transform test results into browsable Maven reports.
- Report command:
BASHmvn surefire-report:report - Useful content: A good report shows pass/fail totals, execution time, failed assertions, and links to details.
- CI use: Publish the generated HTML directory as a build artifact so results remain available after the job finishes.
K. Annotations Used in TestNG
The most frequently used annotations correspond to test execution stages and test metadata.
@Test: Marks a test method or class; attributes includepriority,groups, anddependsOnMethods.@BeforeMethod/@AfterMethod: Run around every test method, making them suitable for browser setup and cleanup.@BeforeClass/@AfterClass: Run around methods in one class.@BeforeSuite/@AfterSuite: Run once for the complete suite.@DataProvider: Supplies named rows of test data to a test method.
L. Validating Tests with Assertions
Assertions compare actual application behavior with an expected result and determine whether a test is correct.
- Hard assertion:
Assert.assertEquals(actual, expected)throws immediately when values differ. - Boolean assertion:
Assert.assertTrue(driver.getTitle().contains("Dashboard"))verifies a condition. - Null assertion:
Assert.assertNotNull(element)checks that an object was located successfully. - Message value:
Assert.assertEquals(actual, expected, "Unexpected page title")improves failure diagnosis. - Soft assertions:
SoftAssertcollects multiple failures and requiressoftAssert.assertAll()to report them; forgetting this call can incorrectly produce a passing test.
M. Creating Multiple Tests
Multiple tests should represent distinct behaviors while sharing only carefully controlled setup.
- Separate methods:
validLogin,invalidLogin, andlogoutcan be independent@Testmethods. - Separate classes: Organize tests by feature, such as
LoginTestandCheckoutTest. - Group selection:
@Test(groups = {"smoke", "login"})permits execution by either category. - Isolation rule: Tests should not rely on data created by a previous test unless dependency is explicit.
- Parallel concern: Shared static drivers or mutable test data can cause race conditions when tests run concurrently.
N. Prioritizing Tests
Priorities influence execution order when tests are otherwise eligible to run.
- Syntax:
@Test(priority = 1)normally runs before@Test(priority = 2). - Default: Tests without a priority generally have priority
0. - Negative values:
priority = -1runs before priority0. - Example:
JAVA@Test(priority = 1) public void createAccount() {} @Test(priority = 2) public void verifyAccount() {} - Design limitation: Priority should not replace test independence; use
dependsOnMethodswhen a real prerequisite exists. - Tie behavior: Tests with equal priority should not be assumed to run in a meaningful business order.
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →