testgen-gitlab
v1.0.2
Published
This project is designed to automate the showing and applying of generated tests for your GitLab projects.
Readme
TestGen Gitlab
This project is designed to automate the showing and applying of generated tests for your GitLab projects.
Prerequisites
- Node.js (version 20 or higher)
- GitLab account with appropriate permissions
Setup
Installation
This tool is available via npx. You can call this it manually with:
npx testgen-gitlabHowever, this tool is meant to be used in the GitLab CI/CD pipeline.
Capabilities
To use this tool in your GitLab CI/CD pipeline, you need to add specific jobs to your .gitlab-ci.yml file.
The tool supports two capabilities, set with the ACTION environment variable:
- post-tests: Converts
test_results.jsonand posts them into the merge request as comments. - apply-tests: Applies the tests that the developer has reacted to after typing "testgen" into the comments.
Example for post-tests
Modify the .gitlab-ci.yml of the repo to add the new stage and job.
stages:
# ... existing stages ...
- generate_tests
# ... Existing jobs ...
generate_tests_job:
stage: generate_tests
needs: []
image: node:20
script:
- wget https://gist.githubusercontent.com/seahyinghang8/22628f087ede28f69f6369af295dd3cb/raw/327813898ea4ef49d13d0449f625152317c6fb35/test_results.json
# Dummy command to simulate test generation
- ACTION="post-tests" npx testgen-gitlab
rules:
- if: $CI_PIPELINE_SOURCE == 'merge_request_event'
when: alwaysExample for apply-tests
Since GitLab doesn't allow specific triggering of jobs, the following .gitlab-ci.yml should live inside an orphaned branch:
stages:
- api_stage
api_only_job:
stage: api_stage
image: node:20
rules:
- if: $CI_PIPELINE_SOURCE == "trigger"
needs: []
variables:
GIT_STRATEGY: none # Skip default cloning
script:
- ACTION="apply-tests" npx testgen-gitlabWebhook Setup
Provision a Pipeline Trigger Token:
- Go to
Settings > CI/CD > Pipeline Trigger Tokens. - Add a new token and call it
testgen token.
- Go to
Add a Webhook:
- Go to
Settings > Webhooksand click "Add new webhook". - For the URL, use:
https://gitlab.com/api/v4/projects/<project-id>/ref/<orphan-branch>/trigger/pipeline?token=<pipeline-trigger-token>. - Under 'Trigger' section, check "Comments".
- Go to
CI/CD Variables Setup
Provision an Access Token:
- Go to
Settings > Access Tokenand click "Add new token". - Use "Testgen" as the token name.
- Select role to be developer and give the following scopes:
api,read_api,read_repository,write_repository.
- Go to
Store the Access Token:
- Go to
Settings > CI/CD > Variablesand store the token asACCESS_TOKEN. - Either choose "masked" or "masked and hidden
- Go to
".
- Untick the "Protect variable" option.
Contributing
To contribute to this project or report issues, please follow the standard GitLab contribution guidelines.
Additional Resources
For more information, refer to the official GitLab documentation.
