RN Detox iOS测试:能否将全系列测试录制成单个视频?
Great question! I’ve dealt with this exact need for a single consolidated video of our full Detox test suite on iOS, and while Detox doesn’t support this natively out of the box, there are reliable workarounds to make it happen:
1. Post-Test Script + FFmpeg Video Merging
This is the most straightforward approach—we’ll let Detox generate individual test videos as usual, then automatically stitch them together into one full-length video after the test run completes.
Step-by-Step Setup:
- Keep using your existing command: Continue running Detox with
--record-videos all(you can also specify a custom artifacts path with--artifacts-location ./my-test-artifactsto keep things organized). Detox will save each test’s video in thevideossubfolder of your artifacts directory. - Install FFmpeg: You’ll need this tool to merge the videos. On macOS, use
brew install ffmpeg; on Linux, usesudo apt-get install ffmpeg. - Create a merge script: Write a simple shell script (e.g.,
scripts/combine_detox_videos.sh) to sort the videos by execution order and merge them:
#!/bin/bash # Navigate to the videos directory (update path if you used --artifacts-location) ARTIFACTS_DIR="./artifacts/videos" cd "$ARTIFACTS_DIR" || exit 1 # Generate a list of videos sorted by modification time (matches test execution order) ls -tr *.mp4 | grep -v "full-test-run" > video_list.txt # Merge all videos into a single file ffmpeg -f concat -safe 0 -i video_list.txt -c copy full-test-run.mp4 # Clean up the temporary list file rm video_list.txt echo "Successfully merged all test videos into full-test-run.mp4"
- Automate the script with Detox hooks: Add a
postTesthook to yourdetox.config.jsto run the script automatically after the entire test suite finishes:
module.exports = { // ... your existing Detox config hooks: { postTest: async () => { const { execSync } = require('child_process'); try { execSync('./scripts/combine_detox_videos.sh', { stdio: 'inherit' }); } catch (error) { console.warn('Warning: Failed to merge test videos. Error:', error.message); } } } };
Notes:
- If you run into encoding issues (e.g., videos won’t merge), replace
-c copyin the FFmpeg command with-c:v libx264 -c:a aac—this re-encodes the videos to a compatible format, though it will take a bit longer. - The
ls -trcommand sorts files by modification time, which aligns with the order your tests ran (since each video is saved right after its test completes).
2. Why Native Support Isn’t Available
Just to clarify: Detox’s iOS video recording relies on Apple’s XCUIRecorder API, which creates a new recording session for each individual XCTest method. This means there’s no way to natively record across multiple test cases in a single session—hence the need for post-processing.
This workaround has been solid for our team, and it lets us keep the per-test videos for debugging while also having a single video for design/validation checks.
内容的提问来源于stack exchange,提问作者Nicolas Smit

