run-tests.sh 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/bin/bash
  2. # Run pytest with recommended options
  3. # Usage: ./run-tests.sh [options]
  4. #
  5. # Options:
  6. # --quick Skip slow tests, minimal output
  7. # --coverage Run with coverage report
  8. # --watch Watch mode with pytest-watch
  9. # --failed Re-run only failed tests
  10. # --debug Enable debug output
  11. set -e
  12. # Colors
  13. RED='\033[0;31m'
  14. GREEN='\033[0;32m'
  15. YELLOW='\033[1;33m'
  16. NC='\033[0m'
  17. # Default options
  18. PYTEST_ARGS="-v"
  19. COVERAGE=""
  20. WATCH=""
  21. # Parse arguments
  22. while [[ $# -gt 0 ]]; do
  23. case $1 in
  24. --quick)
  25. PYTEST_ARGS="-q -x --tb=short"
  26. shift
  27. ;;
  28. --coverage)
  29. COVERAGE="--cov=src --cov-report=term-missing --cov-report=html"
  30. shift
  31. ;;
  32. --watch)
  33. WATCH=1
  34. shift
  35. ;;
  36. --failed)
  37. PYTEST_ARGS="$PYTEST_ARGS --lf"
  38. shift
  39. ;;
  40. --debug)
  41. PYTEST_ARGS="$PYTEST_ARGS -s --tb=long"
  42. shift
  43. ;;
  44. *)
  45. PYTEST_ARGS="$PYTEST_ARGS $1"
  46. shift
  47. ;;
  48. esac
  49. done
  50. # Check if pytest is installed
  51. if ! command -v pytest &> /dev/null; then
  52. echo -e "${RED}pytest not found. Install with: pip install pytest${NC}"
  53. exit 1
  54. fi
  55. # Watch mode
  56. if [[ -n "$WATCH" ]]; then
  57. if ! command -v ptw &> /dev/null; then
  58. echo -e "${YELLOW}pytest-watch not found. Installing...${NC}"
  59. pip install pytest-watch
  60. fi
  61. echo -e "${GREEN}Starting watch mode...${NC}"
  62. ptw -- $PYTEST_ARGS $COVERAGE
  63. exit 0
  64. fi
  65. # Run tests
  66. echo -e "${GREEN}Running pytest...${NC}"
  67. echo "pytest $PYTEST_ARGS $COVERAGE"
  68. echo ""
  69. pytest $PYTEST_ARGS $COVERAGE
  70. # Open coverage report if generated
  71. if [[ -n "$COVERAGE" ]] && [[ -f "htmlcov/index.html" ]]; then
  72. echo ""
  73. echo -e "${GREEN}Coverage report: htmlcov/index.html${NC}"
  74. if command -v open &> /dev/null; then
  75. read -p "Open coverage report? [y/N] " -n 1 -r
  76. echo
  77. if [[ $REPLY =~ ^[Yy]$ ]]; then
  78. open htmlcov/index.html
  79. fi
  80. fi
  81. fi