Running Tests in Docker
When running tests inside Docker containers, you need to extract coverage data from the container to upload it to Qlty. This guide covers two common approaches: using volume mounts and using docker cp
.
Method 1: Volume Mounting
Volume mounting is the simplest approach where you mount your project directory into the container, allowing coverage files to be written directly to your host filesystem.
GitHub Actions Example
Key Points for Volume Mounting
- Mount your current directory (
$(pwd)
) to the container’s working directory - Use
--rm
to automatically remove the container after tests complete - Coverage files are written directly to your host filesystem
- Works with any coverage format supported by Qlty
Method 2: Docker Copy
When you can’t use volume mounts (due to security restrictions or container design), you can copy coverage files out of the container after tests complete.
GitHub Actions Example
Key Points for Docker Copy
- Don’t use
--rm
flag so the container persists after tests - Use
--name
to give the container a predictable name - Copy files using
docker cp container_name:/path/to/file destination
- Clean up the container manually after copying files
Alternative CI Providers
Using Coverage Tokens
If you’re not using GitHub Actions with OIDC, replace the upload step with a coverage token:
Manual CLI Upload
For other CI providers, install the Qlty CLI and upload manually:
For some CI providers you may have to add --override-commit-sha
and --override-branch
flags with commit SHA and branch names.
Multiple Coverage Files
If your Docker container generates multiple coverage files, you can upload them all:
Volume Mount Approach
Docker Copy Approach
Path Fixing
When running tests in Docker, file paths in coverage reports might not match your repository structure. Use path fixing options:
Or with the CLI:
Troubleshooting
Coverage File Not Found
- Ensure your test command actually generates coverage files
- Check that the coverage output directory matches your copy/mount paths
- Verify file permissions allow reading from the container
Path Mismatches
- Use
--strip-prefix
to remove container-specific path prefixes - Use
--add-prefix
if you need to add a path prefix - Check that source paths in coverage reports match your repository structure
Container Cleanup Issues
- Always clean up named containers when using
docker cp
- Use
docker rm -f container_name
to force removal if needed - Consider using
--rm
with volume mounts to avoid cleanup
See Also
- Coverage Tags - Track different types of tests separately
- CI Integration - Automate coverage reporting in your CI pipeline
- Path Fixing - Fix path discrepancies in coverage reports
- Coverage Formats - Supported coverage file formats