Skip to content

Commit 6449d98

Browse files
authored
New Example: myutils (#211)
1 parent 14e2567 commit 6449d98

File tree

7 files changed

+88
-0
lines changed

7 files changed

+88
-0
lines changed

.github/workflows/examples.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,36 @@ jobs:
159159
run: |
160160
export DJANGO_SETTINGS_MODULE=proj.settings
161161
pytest -vv tests -n auto
162+
163+
myutils:
164+
runs-on: ${{ matrix.os }}
165+
166+
strategy:
167+
fail-fast: false
168+
matrix:
169+
python-version: ["3.12"]
170+
os: ["ubuntu-latest"]
171+
172+
steps:
173+
- name: Install apt packages
174+
if: startsWith(matrix.os, 'ubuntu-')
175+
run: |
176+
sudo apt update
177+
- uses: actions/checkout@v4
178+
- name: Set up Python ${{ matrix.python-version }}
179+
uses: actions/setup-python@v5
180+
with:
181+
python-version: ${{ matrix.python-version }}
182+
cache: 'pip'
183+
cache-dependency-path: '**/setup.py'
184+
- name: Install dependencies
185+
working-directory: examples/myutils
186+
run: |
187+
python -m pip install --upgrade pip
188+
pip install -r requirements.txt
189+
190+
- name: Run tests
191+
working-directory: examples/myutils
192+
timeout-minutes: 10
193+
run: |
194+
pytest -vv tests -n auto

examples/myutils/pytest.ini

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[pytest]
2+
log_cli = true
3+
log_cli_level = INFO
4+
log_cli_format = %(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)
5+
log_cli_date_format = %Y-%m-%d %H:%M:%S

examples/myutils/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pytest>=7.4.4
2+
pytest-celery[celery]@git+https://github.com/celery/pytest-celery.git
3+
pytest-xdist>=3.5.0

examples/myutils/tests/__init__.py

Whitespace-only changes.

examples/myutils/tests/conftest.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from types import ModuleType
2+
3+
import pytest
4+
5+
from pytest_celery import CeleryTestWorker
6+
7+
8+
class MyWorker(CeleryTestWorker):
9+
10+
def myfunc(self) -> bool:
11+
exit_code, output = self.container.exec_run(
12+
'python -c "from utils import myfunc; print(myfunc())"',
13+
)
14+
if exit_code != 0:
15+
raise RuntimeError(f"Error: {output}")
16+
output = output.decode("utf-8")
17+
return output.strip()
18+
19+
20+
@pytest.fixture
21+
def default_worker_cls() -> type[CeleryTestWorker]:
22+
return MyWorker
23+
24+
25+
@pytest.fixture
26+
def default_worker_utils_module() -> ModuleType:
27+
from tests import myutils
28+
29+
return myutils

examples/myutils/tests/myutils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from pytest_celery.vendors.worker.content.utils import get_running_processes_info # noqa
2+
3+
4+
def myfunc():
5+
return "foo"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from pytest_celery import CeleryTestSetup
2+
from tests.conftest import MyWorker
3+
4+
5+
def test_myfunc_in_worker(celery_worker: MyWorker):
6+
assert celery_worker.myfunc() == "foo"
7+
assert celery_worker.get_running_processes_info()
8+
9+
10+
def test_myfunc_in_setup_worker(celery_setup: CeleryTestSetup):
11+
celery_worker: MyWorker = celery_setup.worker
12+
assert celery_worker.myfunc() == "foo"
13+
assert celery_worker.get_running_processes_info()

0 commit comments

Comments
 (0)