Skip to content

Commit 19ea187

Browse files
Merge pull request #2323 from VWS-Python/parallel-tests
Make it possible to run all tests in parallel
2 parents 5eef5d1 + 2b4071f commit 19ea187

File tree

4 files changed

+48
-88
lines changed

4 files changed

+48
-88
lines changed

README.rst

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,6 @@ language.
2020
Getting Started
2121
---------------
2222

23-
.. invisible-code-block: python
24-
25-
import contextlib
26-
import pathlib
27-
import shutil
28-
29-
import vws_test_fixtures
30-
from mock_vws import MockVWS
31-
from mock_vws.database import VuforiaDatabase
32-
33-
# We use a low processing time so that tests run quickly.
34-
mock = MockVWS(processing_time_seconds=0.2)
35-
database = VuforiaDatabase(
36-
server_access_key='[server-access-key]',
37-
server_secret_key='[server-secret-key]',
38-
client_access_key='[client-access-key]',
39-
client_secret_key='[client-secret-key]',
40-
)
41-
mock.add_database(database=database)
42-
stack = contextlib.ExitStack()
43-
stack.enter_context(mock)
44-
45-
# We rely on implementation details of the fixtures package.
46-
image = pathlib.Path(vws_test_fixtures.__path__[0]) / 'high_quality_image.jpg'
47-
assert image.exists(), image.resolve()
48-
new_image = pathlib.Path('high_quality_image.jpg')
49-
shutil.copy(image, new_image)
50-
51-
5223
.. code-block:: python
5324
5425
import pathlib
@@ -84,11 +55,6 @@ Getting Started
8455
8556
assert matching_targets[0].target_id == target_id
8657
87-
.. invisible-code-block: python
88-
new_image = pathlib.Path('high_quality_image.jpg')
89-
new_image.unlink()
90-
stack.close()
91-
9258
Full Documentation
9359
------------------
9460

conftest.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
"""Setup for Sybil."""
22

3+
import io
4+
from collections.abc import Generator
35
from doctest import ELLIPSIS
6+
from pathlib import Path
47

58
import pytest
69
from beartype import beartype
10+
from mock_vws import MockVWS
11+
from mock_vws.database import VuforiaDatabase
712
from sybil import Sybil
813
from sybil.parsers.rest import (
914
ClearNamespaceParser,
@@ -21,11 +26,46 @@ def pytest_collection_modifyitems(items: list[pytest.Item]) -> None:
2126
item.obj = beartype(obj=item.obj)
2227

2328

29+
@pytest.fixture
30+
def make_image_file(
31+
high_quality_image: io.BytesIO,
32+
) -> Generator[None, None, None]:
33+
"""
34+
Make an image file available in the test directory.
35+
The path of this file matches the path in the documentation.
36+
"""
37+
new_image = Path("high_quality_image.jpg")
38+
buffer = high_quality_image.getvalue()
39+
new_image.write_bytes(data=buffer)
40+
yield
41+
new_image.unlink()
42+
43+
44+
@pytest.fixture
45+
def mock_vws() -> Generator[None, None, None]:
46+
"""
47+
Yield a mock VWS.
48+
49+
The keys used here match the keys in the documentation.
50+
"""
51+
database = VuforiaDatabase(
52+
server_access_key="[server-access-key]",
53+
server_secret_key="[server-secret-key]",
54+
client_access_key="[client-access-key]",
55+
client_secret_key="[client-secret-key]",
56+
)
57+
# We use a low processing time so that tests run quickly.
58+
with MockVWS(processing_time_seconds=0.2) as mock:
59+
mock.add_database(database=database)
60+
yield
61+
62+
2463
pytest_collect_file = Sybil(
2564
parsers=[
2665
ClearNamespaceParser(),
2766
DocTestParser(optionflags=ELLIPSIS),
2867
PythonCodeBlockParser(),
2968
],
3069
patterns=["*.rst", "*.py"],
70+
fixtures=["make_image_file", "mock_vws"],
3171
).pytest()

docs/source/index.rst

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,6 @@ Usage
1616

1717
See the :doc:`api-reference` for full usage details.
1818

19-
.. invisible-code-block: python
20-
21-
import contextlib
22-
import pathlib
23-
import shutil
24-
25-
import vws_test_fixtures
26-
from mock_vws import MockVWS
27-
from mock_vws.database import VuforiaDatabase
28-
29-
mock = MockVWS(real_http=False)
30-
database = VuforiaDatabase(
31-
server_access_key='[server-access-key]',
32-
server_secret_key='[server-secret-key]',
33-
client_access_key='[client-access-key]',
34-
client_secret_key='[client-secret-key]',
35-
)
36-
mock.add_database(database=database)
37-
stack = contextlib.ExitStack()
38-
stack.enter_context(mock)
39-
40-
# We rely on implementation details of the fixtures package.
41-
image = pathlib.Path(vws_test_fixtures.__path__[0]) / 'high_quality_image.jpg'
42-
assert image.exists(), image.resolve()
43-
new_image = pathlib.Path('high_quality_image.jpg')
44-
shutil.copy(image, new_image)
45-
4619
.. code-block:: python
4720
4821
import pathlib
@@ -62,7 +35,8 @@ See the :doc:`api-reference` for full usage details.
6235
client_access_key=client_access_key,
6336
client_secret_key=client_secret_key,
6437
)
65-
name = 'my_image_name'
38+
import uuid
39+
name = 'my_image_name' + uuid.uuid4().hex
6640
6741
image = pathlib.Path('high_quality_image.jpg')
6842
with image.open(mode='rb') as my_image_file:
@@ -79,12 +53,6 @@ See the :doc:`api-reference` for full usage details.
7953
assert matching_targets[0].target_id == target_id
8054
a = 1
8155
82-
.. invisible-code-block: python
83-
84-
new_image = pathlib.Path('high_quality_image.jpg')
85-
new_image.unlink()
86-
stack.close()
87-
8856
Testing
8957
-------
9058

@@ -96,19 +64,6 @@ To write unit tests for code which uses this library, without using your Vuforia
9664
9765
.. clear-namespace
9866
99-
.. invisible-code-block: python
100-
101-
import pathlib
102-
import shutil
103-
104-
import vws_test_fixtures
105-
106-
# We rely on implementation details of the fixtures package.
107-
image = pathlib.Path(vws_test_fixtures.__path__[0]) / 'high_quality_image.jpg'
108-
assert image.exists(), image.resolve()
109-
new_image = pathlib.Path('high_quality_image.jpg')
110-
shutil.copy(image, new_image)
111-
11267
.. code-block:: python
11368
11469
import pathlib
@@ -129,7 +84,6 @@ To write unit tests for code which uses this library, without using your Vuforia
12984
client_secret_key=database.client_secret_key,
13085
)
13186
132-
13387
image = pathlib.Path('high_quality_image.jpg')
13488
with image.open(mode='rb') as my_image_file:
13589
target_id = vws_client.add_target(
@@ -140,11 +94,6 @@ To write unit tests for code which uses this library, without using your Vuforia
14094
active_flag=True,
14195
)
14296
143-
.. invisible-code-block: python
144-
145-
new_image = pathlib.Path('high_quality_image.jpg')
146-
new_image.unlink()
147-
14897
There are some differences between the mock and the real Vuforia.
14998
See https://vws-python-mock.readthedocs.io/en/latest/differences-to-vws.html for details.
15099

pyproject.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,12 @@ lint.ignore = [
117117
"S101",
118118
]
119119

120-
lint.per-file-ignores."tests/test_*.py" = [
120+
lint.per-file-ignores."conftest.py" = [
121+
# Allow hardcoded secrets in tests.
122+
"S106",
123+
]
124+
125+
lint.per-file-ignores."tests/*.py" = [
121126
# Do not require tests to have a one-line summary.
122127
"D205",
123128
]

0 commit comments

Comments
 (0)