You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Shell脚本中按指定顺序循环调用测试文件

Solution to Run Python Tests in Order with a Shell Script Array

Hey there! It sounds like you're looking to clean up your Shell script by replacing repetitive test execution code with a loop over your existing files_array—great call, that'll make your script way more maintainable and easier to update down the line.

First, let's call out what you might have been doing before (the repetitive code you want to replace):

# Repetitive, hard-to-maintain code
python tests/test_create_01.py
python tests/test_update_02.py
python tests/test_delete_03.py

Here's the streamlined version using your existing array, which will run the tests in the exact order you've defined:

# Your existing array definition (keep this as-is)
files_array=(
    "tests/test_create_01.py"
    "tests/test_update_02.py"
    "tests/test_delete_03.py"
)

# Loop through each test file in the array and execute it
for test_file in "${files_array[@]}"; do
    echo "Starting test: *$test_file*"
    python "$test_file"
    
    # Optional: Stop the script immediately if a test fails (remove if you want all tests to run)
    if [ $? -ne 0 ]; then
        echo "❌ Test failed: $test_file"
        exit 1
    fi
done

echo "✅ All tests completed successfully!"

A few quick notes on why this works well:

  • Using ${files_array[@]} ensures each filename is handled correctly, even if you ever add test files with spaces (a safe practice to follow).
  • The optional failure check gives you control over whether the script stops on the first failed test or keeps going—tweak that block based on your needs.
  • If you need to add more tests later, just append them to the files_array in your desired order—no need to copy-paste the execution code again.

内容的提问来源于stack exchange,提问作者Souad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:23:40