Skip to content

Commit 2d457df

Browse files
authored
chore: Parallelize pytest runs (#889)
* wip * Fix tests * Add parallel tests with xdist * lint * test
1 parent 5fa014d commit 2d457df

File tree

7 files changed

+51
-7
lines changed

7 files changed

+51
-7
lines changed

.github/workflows/backend_unit_tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ jobs:
3939
- name: Test with pytest
4040
if: github.actor != 'dependabot[bot]'
4141
run: |
42-
make run-unit-tests
42+
make run-unit-tests-debug
4343
env:
4444
PYTHONPATH: src
4545
- name: Upload coverage reports to Codecov

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,18 @@ exec-terrarium:
3939
# Testing & Linting
4040
.PHONY: run-unit-tests
4141
run-unit-tests:
42+
poetry run pytest -n auto src/backend/tests/unit/$(file) --cov=src/backend --cov-report=xml
43+
44+
.PHONY: run-unit-tests-debug
45+
run-unit-tests-debug:
4246
poetry run pytest src/backend/tests/unit/$(file) --cov=src/backend --cov-report=xml
4347

4448
.PHONY: run-community-tests
4549
run-community-tests:
50+
poetry run pytest -n auto src/community/tests/$(file) --cov=src/community --cov-report=xml
51+
52+
.PHONY: run-community-tests-debug
53+
run-community-tests-debug:
4654
poetry run pytest src/community/tests/$(file) --cov=src/community --cov-report=xml
4755

4856
.PHONY: run-integration-tests

poetry.lock

Lines changed: 35 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ freezegun = "^1.5.1"
7373
pre-commit = "^2.20.0"
7474
ruff = "^0.6.0"
7575
pytest-asyncio = "^0.23.7"
76+
pytest-xdist = "^3.6.1"
7677

7778
[tool.poetry.group.setup]
7879
optional = true

src/backend/tests/unit/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Writing Unit Test
1+
# Writing Unit Tests
22

33
This README will explain how to best write unit tests for the OSS Toolkit project.
44

@@ -10,7 +10,7 @@ make test-db
1010
make run-unit-tests
1111
```
1212

13-
## Test DB
13+
## Testing the Database
1414

1515
Some of the unit tests rely on a database being present. The tests are expecting a postgres DB to be running in a docker container. the `make test-db` target will create the test database required for the tests to run.
1616

src/backend/tests/unit/tools/test_lang_chain.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from backend.services.context import Context
88
from backend.tools import LangChainVectorDBRetriever, LangChainWikiRetriever
9+
from backend.tools.base import ToolError, ToolErrorCode
910

1011
is_cohere_env_set = (
1112
os.environ.get("COHERE_API_KEY") is not None
@@ -78,7 +79,7 @@ async def test_wiki_retriever_no_docs() -> None:
7879
):
7980
result = await retriever.call({"query": query}, ctx)
8081

81-
assert result == [{'details': '','success': False,'text': 'No results found.','type': 'other'}]
82+
assert result == ToolError(type=ToolErrorCode.OTHER, success=False, text='No results found.', details='No results found for the given params.')
8283

8384

8485

@@ -164,4 +165,4 @@ async def test_vector_db_retriever_no_docs() -> None:
164165
mock_db.as_retriever().get_relevant_documents.return_value = mock_docs
165166
result = await retriever.call({"query": query}, ctx)
166167

167-
assert result == [{'details': '', 'success': False, 'text': 'No results found.', 'type': 'other'}]
168+
assert result == ToolError(type=ToolErrorCode.OTHER, success=False, text='No results found.', details='No results found for the given params.')

src/backend/tools/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def _get_class_specific_preamble(cls, tool_class_id):
104104
preamble = f"If you decide to use the toolkit_python_interpreter tool and plan to plot something, try returning the result as a PNG. Ensure that the generated code does not require an internet connection. Avoid using the following packages in code generation: {forbidden_preamble_packages}. "
105105
return preamble
106106
except Exception as e:
107-
logger.error(f"Error while retrieving the Python interpreter preamble.: {str(e)}")
107+
logger.error(event=f"Error while retrieving the Python interpreter preamble: {str(e)}")
108108
return None
109109

110110
return None

0 commit comments

Comments
 (0)