Skip to content

Commit b3fcd8a

Browse files
shukladivyanshcopybara-github
authored andcommitted
feat: Add ability to run individual unit tests to unittests.sh
PiperOrigin-RevId: 888314229
1 parent 995cd1c commit b3fcd8a

File tree

1 file changed

+37
-20
lines changed

1 file changed

+37
-20
lines changed

scripts/unittests.sh

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
# Runs all unit tests for adk codebase. Sets up test environment according to
1818
# CONTRIBUTING.md.
19-
# Usage: ./unittests.sh [--version <version>]
19+
# Usage: ./unittests.sh [--version <version>] [pytest_args]
2020

2121
set -euo pipefail
2222

@@ -27,25 +27,33 @@ cd ..
2727
# Argument Parsing
2828
ALL_VERSIONS=("3.10" "3.11" "3.12" "3.13" "3.14")
2929
versions_to_run=()
30+
PYTEST_ARGS=()
31+
32+
while [[ $# -gt 0 ]]; do
33+
case "$1" in
34+
--version)
35+
if [[ -z "${2:-}" ]]; then
36+
echo "Error: Missing version for --version flag." >&2
37+
echo "Usage: $0 [--version <version>] [pytest_args]" >&2
38+
exit 1
39+
fi
40+
# Validate version
41+
if ! [[ " ${ALL_VERSIONS[*]} " =~ " $2 " ]]; then
42+
echo "Error: Invalid version '$2'. Supported versions: ${ALL_VERSIONS[*]}" >&2
43+
exit 1
44+
fi
45+
versions_to_run=("$2")
46+
shift 2
47+
;;
48+
*)
49+
PYTEST_ARGS+=("$1")
50+
shift
51+
;;
52+
esac
53+
done
3054

31-
if [[ $# -eq 0 ]]; then
55+
if [[ ${#versions_to_run[@]} -eq 0 ]]; then
3256
versions_to_run=("${ALL_VERSIONS[@]}")
33-
elif [[ "$1" == "--version" ]]; then
34-
if [[ -z "${2:-}" ]]; then
35-
echo "Error: Missing version for --version flag." >&2
36-
echo "Usage: $0 --version <version>" >&2
37-
exit 1
38-
fi
39-
# Validate version
40-
if ! [[ " ${ALL_VERSIONS[*]} " =~ " $2 " ]]; then
41-
echo "Error: Invalid version '$2'. Supported versions: ${ALL_VERSIONS[*]}" >&2
42-
exit 1
43-
fi
44-
versions_to_run=("$2")
45-
else
46-
echo "Error: Unknown argument '$1'." >&2
47-
echo "Usage: $0 [--version <version>]" >&2
48-
exit 1
4957
fi
5058

5159

@@ -104,10 +112,19 @@ for version in "${versions_to_run[@]}"; do
104112
echo "Setting up test environment in $VENV_DIR..."
105113
uv sync --extra test --active --frozen
106114

115+
# Determine if PYTEST_ARGS contains explicit test targets (e.g. file, dir, or node ID)
116+
HAS_TEST_TARGET=false
117+
for arg in "${PYTEST_ARGS[@]:-}"; do
118+
[[ "$arg" == *.py || -d "$arg" || "$arg" == *::* ]] && HAS_TEST_TARGET=true && break
119+
done
120+
107121
echo "Running unit tests..."
108122
TEST_EXIT_CODE=0
109-
pytest ./tests/unittests || TEST_EXIT_CODE=$?
110-
123+
if [[ "$HAS_TEST_TARGET" == true ]]; then
124+
pytest "${PYTEST_ARGS[@]}" || TEST_EXIT_CODE=$?
125+
else
126+
pytest ./tests/unittests "${PYTEST_ARGS[@]:-}" || TEST_EXIT_CODE=$?
127+
fi
111128
# 4. report the unit tests status as is
112129
if [[ $TEST_EXIT_CODE -ne 0 ]]; then
113130
echo ""

0 commit comments

Comments
 (0)