In this exercise, you will learn how to integrate automated tests into your CI pipeline. Automated testing is a crucial part of CI/CD as it ensures that your code changes do not break existing functionality and helps maintain code quality.
Objectives
- Understand the importance of automated testing in CI/CD.
- Learn how to set up automated tests in a CI pipeline.
- Execute and verify automated tests using a CI tool.
Prerequisites
- Basic understanding of CI/CD concepts.
- Familiarity with a CI tool (e.g., Jenkins, GitLab CI/CD, CircleCI).
- A sample project with some existing tests (e.g., a simple Node.js application).
Steps to Integrate Automated Tests
Step 1: Set Up Your CI Environment
Ensure you have a CI environment set up. If you haven't done this yet, refer to the "Setting Up a CI Environment" section in Module 2.
Step 2: Add Test Scripts to Your Project
Ensure your project has test scripts. For example, in a Node.js project, you might use a testing framework like Mocha or Jest.
Example: Adding Jest to a Node.js Project
-
Install Jest:
npm install --save-dev jest
-
Add a test script to your
package.json
:{ "scripts": { "test": "jest" } }
-
Create a simple test file,
sum.test.js
:const sum = require('./sum'); test('adds 1 + 2 to equal 3', () => { expect(sum(1, 2)).toBe(3); });
-
Create the
sum.js
file:function sum(a, b) { return a + b; } module.exports = sum;
Step 3: Configure the CI Tool to Run Tests
Configure your CI tool to run the tests as part of the pipeline.
Example: Configuring Jenkins
- Create a new Jenkins job or pipeline.
- In the pipeline configuration, add a stage to run tests.
Jenkins Pipeline Script Example:
pipeline { agent any stages { stage('Checkout') { steps { git 'https://github.com/your-repo/your-project.git' } } stage('Install Dependencies') { steps { sh 'npm install' } } stage('Run Tests') { steps { sh 'npm test' } } } }
Step 4: Execute the Pipeline
Run the pipeline and verify that the tests are executed.
- Trigger the pipeline manually or push a commit to the repository to trigger it automatically.
- Monitor the pipeline execution in the CI tool's dashboard.
- Check the test results in the pipeline logs.
Step 5: Verify Test Results
Ensure that the tests pass successfully. If any tests fail, investigate the cause and fix the issues.
Common Issues and Solutions:
- Missing Dependencies: Ensure all required dependencies are installed.
- Incorrect Test Configuration: Verify that the test framework is correctly configured.
- Code Errors: Debug and fix any errors in the code or tests.
Practical Exercise
Exercise: Integrate Automated Tests in a CI Pipeline
-
Set Up a Sample Project:
- Create a simple Node.js project with a few test cases using Jest.
-
Configure Jenkins:
- Set up a Jenkins pipeline to run the tests.
-
Run the Pipeline:
- Execute the pipeline and verify that the tests run successfully.
Solution
Sample Node.js Project Structure:
package.json:
{ "name": "my-nodejs-app", "version": "1.0.0", "scripts": { "test": "jest" }, "devDependencies": { "jest": "^27.0.0" } }
sum.js:
sum.test.js:
Jenkins Pipeline Script:
pipeline { agent any stages { stage('Checkout') { steps { git 'https://github.com/your-repo/your-project.git' } } stage('Install Dependencies') { steps { sh 'npm install' } } stage('Run Tests') { steps { sh 'npm test' } } } }
Conclusion
In this exercise, you learned how to integrate automated tests into a CI pipeline. Automated testing is essential for maintaining code quality and ensuring that new changes do not introduce bugs. By following the steps outlined, you can set up and run automated tests in your CI environment, helping you to catch issues early in the development process.
Next, you will learn about deploying your application in a production environment in the next exercise.
CI/CD Course: Continuous Integration and Deployment
Module 1: Introduction to CI/CD
Module 2: Continuous Integration (CI)
- Introduction to Continuous Integration
- Setting Up a CI Environment
- Build Automation
- Automated Testing
- Integration with Version Control
Module 3: Continuous Deployment (CD)
- Introduction to Continuous Deployment
- Deployment Automation
- Deployment Strategies
- Monitoring and Feedback
Module 4: Advanced CI/CD Practices
Module 5: Implementing CI/CD in Real Projects
Module 6: Tools and Technologies
Module 7: Practical Exercises
- Exercise 1: Setting Up a Basic Pipeline
- Exercise 2: Integrating Automated Tests
- Exercise 3: Deployment in a Production Environment
- Exercise 4: Monitoring and Feedback