This guide will walk you through the process of setting up automated testing with Cucumber on AWS EC2 instances. We’ll cover the setup of an EC2 instance, installation of necessary software, configuring Cucumber, and running automated tests.
Prerequisites
- AWS Account: Ensure you have an AWS account.
- IAM Role: Create an IAM role with necessary permissions for EC2.
- SSH Key Pair: Create an SSH key pair for accessing the EC2 instance.
Step 1: Launch an EC2 Instance
- Log in to AWS Management Console: Navigate to the EC2 Dashboard.
- Launch Instance: Click on “Launch Instance”.
- Choose AMI: Select an Amazon Linux 2 AMI (or another preferred OS).
- Instance Type: Choose an instance type (e.g., t2.micro for a basic setup).
- Configure Instance: Configure your instance details. Ensure the IAM role is attached if needed.
- Add Storage: Default settings are usually sufficient.
- Add Tags: Tag your instance (optional).
- Configure Security Group: Create a new security group or use an existing one. Allow SSH (port 22) from your IP.
- Review and Launch: Review your settings and launch the instance. Select the key pair you created for SSH access.
Step 2: SSH into the EC2 Instance
- Get Public DNS: Obtain the public DNS of your instance from the EC2 Dashboard.
- SSH Command: Use the following command to SSH into your instance:shCopy code
ssh -i /path/to/your-key-pair.pem ec2-user@your-ec2-public-dns
ssh -i /path/to/your-key-pair.pem ec2-user@your-ec2-public-dns
Step 3: Install Necessary Software
Once connected to the EC2 instance, install Java, Maven, and other dependencies.
Update the Package Repository:
sudo yum update -y
Install Java:
sudo yum install java-1.8.0-openjdk-devel -y
Install Maven:
sudo yum install maven -y
Install Git:
sudo yum install git -y
Step 4: Set Up a Cucumber Project
Create a New Directory:
mkdir cucumber-test
cd cucumber-test
Initialize a Maven Project:
mvn archetype:generate -DgroupId=com.example -DartifactId=cucumber-test -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
cd cucumber-test
Edit the pom.xml
: Add Cucumber dependencies.
<dependencies>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>6.10.4</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>6.10.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
Step 5: Write Cucumber Tests
Create Feature File:
mkdir -p src/test/resources/features
nano src/test/resources/features/example.feature
Example content for example.feature
:
Feature: Example feature
Scenario: Example scenario
Given I have an example
When I execute the example
Then the example should be successful
Step Definitions:
mkdir -p src/test/java/com/example/steps
nano src/test/java/com/example/steps/ExampleSteps.java
Example content for ExampleSteps.java
:
package com.example.steps;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import io.cucumber.java.en.Then;
import static org.junit.Assert.assertTrue;
public class ExampleSteps {
@Given("I have an example")
public void i_have_an_example() {
// Setup code here
}
@When("I execute the example")
public void i_execute_the_example() {
// Execution code here
}
@Then("the example should be successful")
public void the_example_should_be_successful() {
assertTrue(true); // Assertion here
}
}
Test Runner:
nano src/test/java/com/example/RunCucumberTest.java
Example content for RunCucumberTest.java
:
package com.example;
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources/features", glue = "com.example.steps")
public class RunCucumberTest {
}
Step 6: Running Tests
Compile the Project:
mvn compile
Run Cucumber Tests:
mvn test
Step 7: Automating Tests with CI/CD
To fully automate the testing process, integrate it with a CI/CD pipeline. Jenkins is a common tool for this purpose. Here’s a brief outline on setting it up:
Install Jenkins
Add Jenkins Repository:
sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo
sudo rpm --import http://pkg.jenkins-ci.org/redhat/jenkins-ci.org.key
Install Jenkins:
sudo yum install jenkins -y
sudo service jenkins start
Open Jenkins Port:
sudo iptables -A INPUT -p tcp --dport 8080 -j ACCEPT
sudo service iptables save
Access Jenkins:
Navigate to http://your-ec2-public-dns:8080
to complete the setup.
Configure Jenkins Job
- Create a New Job: Choose “Freestyle project”.
- Configure Source Code Management: Add your Git repository.
- Build Steps: Add a build step to run your tests.
- Save and Build Now: Save the job and trigger a build to see your tests run automatically.
Considerations and Best Practices
- Instance Type: Choose an appropriate instance type based on your test requirements.
- Security: Ensure security groups and IAM roles are properly configured to avoid unauthorized access.
- Scaling: Use auto-scaling groups for larger projects to handle test loads.
- Monitoring: Integrate AWS CloudWatch for monitoring your tests and EC2 instances.
- Backups: Regularly backup your Jenkins configuration and Cucumber test data.
By following this guide, you should be able to set up automated testing with Cucumber on AWS EC2 instances, ensuring a robust and scalable testing environment for your applications.