Skip to content

Commit 4764d07

Browse files
authored
Improve pytest speed (#1440)
* WIP: revise test scope, mark expected speed * WIP: refactor tests for mocking dependencies * WIP: mock deps 2 * WIP: blackify * WIP: mock deps 3 * WIP: mock deps 4 * blackify * WIP: isolate mocks, remove prints * WIP: isolate mocks 2 * WIP: fix fails * WIP: fix fails 2 * WIP: fix decoding mocking 1 * WIP: fix decoding mocking 2 * Rotate container name and port by branch name * WIP: fix decoding mock 3 * WIP: Fix decoding mocks 4 * WIP: Fix decoding mocks 5 * WIP: Skip rotate container name in gh-actions * blackify * Fix default port when --no-docker * Fix default port when --no-docker * WIP: fix decoder mock 6 * Refactor common recompute test code * WIP: fix refactor * Revise gh-action tests. Fast on push, full on merge * Fix merge conflict * Blackify * Update tests doc * PR comments * Update changelog * Update pyproject * declare_all_merge_tables: List -> Tuple
1 parent b17c225 commit 4764d07

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+2221
-340
lines changed

.github/workflows/test-conda.yml

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,29 @@ on:
1010
schedule: # once a day at midnight UTC
1111
- cron: '0 0 * * *'
1212
pull_request: # requires approval for first-time contributors
13-
types: [synchronize, opened, reopened, labeled]
13+
types: [synchronize, opened, reopened, labeled, closed]
1414
workflow_dispatch: # Manually trigger with 'Run workflow' button
15+
inputs:
16+
test_mode:
17+
description: 'Test mode (fast or full)'
18+
required: false
19+
default: 'full'
20+
type: choice
21+
options:
22+
- fast
23+
- full
1524

1625
concurrency: # Replace Cancel Workflow Action
1726
group: ${{ github.workflow }}-${{ github.ref }}
1827
cancel-in-progress: true
1928

2029
jobs:
2130
run-tests:
31+
# Skip if PR is closed but not merged
32+
if: |-
33+
github.event_name != 'pull_request' ||
34+
github.event.action != 'closed' ||
35+
github.event.pull_request.merged == true
2236
runs-on: ubuntu-latest
2337
defaults:
2438
run:
@@ -29,12 +43,17 @@ jobs:
2943
services:
3044
mysql:
3145
image: datajoint/mysql:8.0
32-
env: # args: mysql -h 127.0.0.1 -P 3308 -uroot -ptutorial -e "CMD;"
46+
# args: mysql -h 127.0.0.1 -P 3308 -uroot -ptutorial -e "CMD;"
47+
env:
3348
MYSQL_DATABASE: localhost
3449
MYSQL_ROOT_PASSWORD: tutorial
3550
ports:
3651
- 3308:3306
37-
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
52+
options: >-
53+
--health-cmd="mysqladmin ping"
54+
--health-interval=10s
55+
--health-timeout=5s
56+
--health-retries=3
3857
steps:
3958
- name: Checkout
4059
uses: actions/checkout@v4
@@ -52,12 +71,26 @@ jobs:
5271
- name: Install apt dependencies
5372
run: |
5473
sudo apt-get update # First mysql options
55-
sudo apt-get install mysql-client libmysqlclient-dev libgirepository1.0-dev -y
56-
sudo apt-get install ffmpeg libsm6 libxext6 -y # non-dlc position deps
74+
# non-dlc position deps
75+
sudo apt-get install \
76+
mysql-client libmysqlclient-dev libgirepository1.0-dev -y
77+
sudo apt-get install ffmpeg libsm6 libxext6 -y
5778
- name: Run pip install for test deps
5879
run: |
5980
pip install --quiet .[test]
81+
- name: Cache test data
82+
id: cache-test-data
83+
uses: actions/cache@v4
84+
with:
85+
path: |
86+
tests/_data/raw/
87+
tests/_data/video/
88+
key: |-
89+
test-data-${{ hashFiles('.github/workflows/test-conda.yml') }}-v1
90+
restore-keys: |
91+
test-data-
6092
- name: Download data
93+
if: steps.cache-test-data.outputs.cache-hit != 'true'
6194
env:
6295
BASEURL: https://ucsf.box.com/shared/static/
6396
NWB_URL: k3sgql6z475oia848q1rgms4zdh4rkjn.nwb
@@ -78,12 +111,51 @@ jobs:
78111
curl_opts $VID_DIR $VID_TWO $VID2URL
79112
- name: Move actions coveragerc
80113
run: mv tests/.coveragerc .coveragerc
81-
- name: Run tests
114+
- name: Determine test mode
115+
id: test_mode
116+
run: |
117+
# Determine which tests to run based on trigger
118+
if [[ "${{ github.event_name }}" == "push" ]]; then
119+
# Fast tests on push
120+
echo "mode=fast" >> $GITHUB_OUTPUT
121+
echo "marker_args=-m 'not slow and not very_slow'" \
122+
>> $GITHUB_OUTPUT
123+
echo "description=Fast tests" >> $GITHUB_OUTPUT
124+
elif [[ "${{ github.event_name }}" == "pull_request" ]] \
125+
&& [[ "${{ github.event.action }}" == "closed" ]] \
126+
&& [[ "${{ github.event.pull_request.merged }}" == "true" ]]; then
127+
# Full suite on PR merge
128+
echo "mode=full" >> $GITHUB_OUTPUT
129+
echo "marker_args=" >> $GITHUB_OUTPUT
130+
echo "description=Full test suite" >> $GITHUB_OUTPUT
131+
elif [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
132+
# Manual trigger - use input
133+
if [[ "${{ github.event.inputs.test_mode }}" == "fast" ]]; then
134+
echo "mode=fast" >> $GITHUB_OUTPUT
135+
echo "marker_args=-m 'not slow and not very_slow'" \
136+
>> $GITHUB_OUTPUT
137+
echo "description=Fast tests (manual)" >> $GITHUB_OUTPUT
138+
else
139+
echo "mode=full" >> $GITHUB_OUTPUT
140+
echo "marker_args=" >> $GITHUB_OUTPUT
141+
echo "description=Full suite (manual)" >> $GITHUB_OUTPUT
142+
fi
143+
else
144+
# Full suite for schedule and other PR events
145+
# (synchronize, opened, etc.)
146+
echo "mode=full" >> $GITHUB_OUTPUT
147+
echo "marker_args=" >> $GITHUB_OUTPUT
148+
echo "description=Full test suite" >> $GITHUB_OUTPUT
149+
fi
150+
- name: Run tests (${{ steps.test_mode.outputs.description }})
82151
run: |
83152
pytest --no-docker --no-dlc \
84153
--cov-config=.coveragerc --cov=spyglass-neuro --cov-report=xml \
154+
${{ steps.test_mode.outputs.marker_args }} \
85155
tests/
86156
- name: Upload coverage reports to Codecov
87157
uses: codecov/codecov-action@v5
88158
with:
89159
token: ${{ secrets.CODECOV_TOKEN }}
160+
flags: ${{ steps.test_mode.outputs.mode }}-tests
161+
name: ${{ steps.test_mode.outputs.description }}

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ DecodingParameters().alter()
4141
- Simplify PR template #1370
4242
- Add `SpyglassIngestion` class to centralize functionality #1377, #1423, #1465
4343
- Pin `ndx-optogenetics` to 0.2.0 #1458
44+
- Refactor pytests for speed, run fast tests on push #1440
4445

4546
### Pipelines
4647

0 commit comments

Comments
 (0)