0
0
JUnittesting~8 mins

CI pipeline test stage in JUnit - Framework Patterns

Choose your learning style9 modes available
Framework Mode - CI pipeline test stage
Folder Structure
my-project/
├── src/
│   ├── main/
│   │   └── java/
│   │       └── com/example/app/
│   │           └── Application.java
│   └── test/
│       └── java/
│           └── com/example/app/
│               ├── pages/
│               │   └── LoginPage.java
│               ├── tests/
│               │   └── LoginTest.java
│               └── utils/
│                   └── TestUtils.java
├── pom.xml
├── Jenkinsfile
└── README.md
    
Test Framework Layers
  • Driver Layer: Manages WebDriver setup and teardown for browser automation.
  • Page Objects: Classes representing UI pages with methods to interact with page elements.
  • Tests: JUnit test classes containing test methods with assertions.
  • Utilities: Helper classes for common functions like waits, data handling, and logging.
  • Configuration: External files and code to manage environment variables, browser types, and credentials.
Configuration Patterns

Use pom.xml for dependencies and plugins.

Use src/test/resources/config.properties to store environment URLs, browser names, and credentials securely.

Use system properties or environment variables to switch environments and browsers during CI runs.

Example snippet in pom.xml to pass parameters:

<properties>
  <env>staging</env>
  <browser>chrome</browser>
</properties>

In tests, read these properties to configure test execution dynamically.

Test Reporting and CI/CD Integration
  • Use JUnit's built-in XML reports for test results.
  • Integrate with Jenkins or other CI tools to run tests automatically on code commits.
  • Configure Jenkinsfile to include a test stage that runs mvn test and archives reports.
  • Use plugins like JUnit Plugin in Jenkins to visualize test results and trends.
  • Fail the pipeline if tests fail to prevent bad code from deploying.
Best Practices
  1. Keep tests isolated and independent to avoid flaky failures.
  2. Use explicit waits to handle dynamic web elements reliably.
  3. Store sensitive data like credentials outside source code, use environment variables or secure vaults.
  4. Run tests in parallel in CI to reduce feedback time.
  5. Keep the CI pipeline fast by running only necessary tests on each commit.
Self Check

Question: In this framework structure, where would you add a new page object class for the "Dashboard" page?

Answer: Add the new DashboardPage.java class inside the src/test/java/com/example/app/pages/ folder.

Key Result
Organize tests with clear layers, configure environments flexibly, and integrate automated test runs in CI pipelines for reliable feedback.