API Upload Testing: A Comprehensive Guide to End-to-End Testing with TestNG and Kotlin
This guide provides a comprehensive overview of how to perform end-to-end testing for Upload APIs using Kotlin, Gradle, and the TestNG framework. Learn how to set up your environment, run tests, filter test cases, and leverage data providers for robust and efficient API testing.
Master End-to-End API Testing
This guide helps you master end-to-end testing, ensuring your Upload API functions flawlessly across various scenarios. We use real file uploads to verify metadata, file copying, processing status API integration, and overall health. Aimed at validating releases in DEV, TEST, or STAGE environments, or for frequent functionality checks, this approach also supports local environments.
Local Environment Setup for API Testing
Before diving into testing, you'll need to set up your local environment. Install the following tools to get started with Upload API testing:
- Java JDK 17: Required to run the Kotlin code.
- Gradle: A build automation tool used for dependency management and running tests.
Windows Tip: Use the gradlew
batch file within the repository to execute Gradle commands if you have Gradle installed.
Install Gradle Dependencies
Run gradle build
to install all project dependencies. This includes TestNG and other necessary components.
Note: Temporarily disable Zscalar if you encounter issues during dependency installation.
Configure Environment Variables for Seamless Testing
Copy local.properites-example
to local.properties
in the project's root directory. This file holds crucial environment variables.
The EnvConfig
class (src/test/kotlin/util/EnvConfig.kt
) reads configurations from local.properties
. This setup allows you to conveniently manage environment-specific settings like URLs and credentials.
Executing API Tests: Maximize Efficiency and Precision
Tests reside in the /src/test/kotlin
directory, organized into test suites based on functional areas. Execute these tests using Gradle, with options for command-line parameters.
Optional Parameters for Targeted Testing
- manifestFilter: Select specific test cases using a comma-separated list within a JSON file in the
resources
folder. Without this, tests run for all use cases.
gradle test -PmanifestFilter='data_stream_id=ehdi'
gradle test -PmanifestFilter='meta_destination_id=ndlp&meta_ext_source=IZGW'
gradle test -PmanifestFilter='jurisdiction=AKA,CA;data_stream_route=csv,other&sender_id=CA-ABCs,IZGW'
Practical Examples: Run Tests Your Way
- Run All Tests: (Command not provided, but generally
gradle test
) - Run tests from a specific file:
gradle test --tests "FileCopy"
- Run tests from a specific file and a specific test:
gradle test --tests "FileCopy.shouldUploadFile"
gradle test --tests "FileCopy.shouldUploadFile"
Tips for Efficient Testing:
- Use
--tests
to target specific test files or individual tests for focused execution. - The
--rerun
command forces tests to rerun.
Data-Driven Testing Using TestNG Data Providers
This project incorporates a DataProvider
utility class, crucial for feeding test data to TestNG tests. These data-driven test cases allow for repeated test execution across various scenarios defined in a JSON source.
Understanding Data Provider Definitions
The @DataProvider
decorator predefines the data provider for each test, specifying how data is injected. Defined data providers are located in /src/test/kotlin/util/DataProvider.kt
.
Enhancing Testing Efficiency in the Future: Parallelization
Future improvements will focus on parallelization for data-provided test cases. This will significantly reduce test execution time and improve overall efficiency.