Skip to content

Commit e5ebb9c

Browse files
authored
Add test filter in run_tests.sh. (sonic-net#13017)
* Add test filter in run_tests.sh.
1 parent b4d1e9b commit e5ebb9c

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

docs/tests/pytest.run.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,20 @@
1616
* executed t1 test cases.
1717

1818

19-
### Run a single test case ###
19+
### Run all tests in a single test file ###
2020
* ./run_tests.sh -d <dut_name> -n <testbed_name> [-s <list of test cases or files to skip>] -u -c platform_tests/test_link_flap.py
2121
* execute link flap test case.
2222
* when specifying test cast list, no need to specify topology with -t.
2323

24+
### Run test cases in a single test file with filter ###
25+
* ./run_tests.sh -d <dut_name> -n <testbed_name> [-s <list of test cases or files to skip>] -u -c arp/test_arpall.py -C garp
26+
* execute all tests that has garp in the name in arp/test_arpall.py
27+
* ./run_tests.sh -d <dut_name> -n <testbed_name> [-s <list of test cases or files to skip>] -u -c arp/test_arpall.py -C "garp or unicast"
28+
* execute all tests that has garp or unicast in the name in arp/test_arpall.py
29+
* ./run_tests.sh -d <dut_name> -n <testbed_name> [-s <list of test cases or files to skip>] -u -c arp/test_arpall.py -C "not garp"
30+
* execute all tests that don't have garp in the name in arp/test_arpall.py
31+
* For more information on the filter, please refer to the [pytest documentation](https://docs.pytest.org/en/latest/example/markers.html#using-k-expr-to-select-tests-based-on-their-name)
32+
2433
### Run scripts under a folder ###
2534
* ./run_tests.sh -d <dut_name> -n <testbed_name> -u -c "snmp/test_*.py" -s "snmp/test_snmp_cpu.py"
2635
* execute all the scripts under `snmp`, skip script `snmp/test_snmp_cpu.py`.

tests/run_tests.sh

+10-1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ function setup_environment()
103103
SKIP_FOLDERS="ptftests acstests saitests scripts k8s sai_qualify"
104104
TESTBED_FILE="${BASE_PATH}/ansible/testbed.yaml"
105105
TEST_CASES=""
106+
TEST_FILTER=""
106107
TEST_INPUT_ORDER="False"
107108
TEST_METHOD='group'
108109
TEST_MAX_FAIL=0
@@ -193,6 +194,10 @@ function setup_test_options()
193194
fi
194195
done
195196

197+
if [[ ! -z $TEST_FILTER ]]; then
198+
PYTEST_COMMON_OPTS="${PYTEST_COMMON_OPTS} -k ${TEST_FILTER}"
199+
fi
200+
196201
if [[ -d ${LOG_PATH} ]]; then
197202
rm -rf ${LOG_PATH}
198203
fi
@@ -245,6 +250,7 @@ function run_debug_tests()
245250
echo "SKIP_SCRIPTS: ${SKIP_SCRIPTS}"
246251
echo "SKIP_FOLDERS: ${SKIP_FOLDERS}"
247252
echo "TEST_CASES: ${TEST_CASES}"
253+
echo "TEST_FILTER: ${TEST_FILTER}"
248254
echo "TEST_INPUT_ORDER: ${TEST_INPUT_ORDER}"
249255
echo "TEST_MAX_FAIL: ${TEST_MAX_FAIL}"
250256
echo "TEST_METHOD: ${TEST_METHOD}"
@@ -348,7 +354,7 @@ function run_individual_tests()
348354
setup_environment
349355

350356

351-
while getopts "h?a:b:c:d:e:Ef:i:I:k:l:m:n:oOp:q:rs:S:t:ux" opt; do
357+
while getopts "h?a:b:c:C:d:e:Ef:i:I:k:l:m:n:oOp:q:rs:S:t:ux" opt; do
352358
case ${opt} in
353359
h|\? )
354360
show_help_and_exit 0
@@ -363,6 +369,9 @@ while getopts "h?a:b:c:d:e:Ef:i:I:k:l:m:n:oOp:q:rs:S:t:ux" opt; do
363369
c )
364370
TEST_CASES="${TEST_CASES} ${OPTARG}"
365371
;;
372+
C )
373+
TEST_FILTER="${TEST_FILTER} ${OPTARG}"
374+
;;
366375
d )
367376
DUT_NAME=${OPTARG}
368377
;;

0 commit comments

Comments
 (0)