Skip to content

Commit b045532

Browse files
committed
clean up test module structure and pytest hooks
1 parent b363b52 commit b045532

10 files changed

Lines changed: 78 additions & 95 deletions

File tree

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,12 @@
11
"""Global pytest configuration for the Airbyte CDK tests."""
22

3-
from pathlib import Path
4-
from typing import cast
5-
63
import pytest
74

85

9-
def pytest_collect_file(parent: pytest.Module | None, path: Path) -> pytest.Module | None:
10-
"""Collect test files based on their names."""
11-
if path.name == "test_connector.py":
12-
return cast(pytest.Module, pytest.Module.from_parent(parent, path=path))
13-
14-
return None
15-
16-
176
def pytest_configure(config: pytest.Config) -> None:
18-
config.addinivalue_line("markers", "connector: mark test as a connector test")
19-
20-
21-
def pytest_addoption(parser: pytest.Parser) -> None:
22-
parser.addoption(
23-
"--run-connector",
24-
action="store_true",
25-
default=False,
26-
help="run connector tests",
7+
config.addinivalue_line(
8+
name="markers",
9+
line="requires_creds: mark test as a test that requires credentials",
2710
)
2811

2912

@@ -34,13 +17,3 @@ def pytest_collection_modifyitems(config: pytest.Config, items: list[pytest.Item
3417
for item in items:
3518
if "connector" in item.keywords:
3619
item.add_marker(skip_connector)
37-
38-
39-
def pytest_runtest_setup(item: pytest.Item) -> None:
40-
# This hook is called before each test function is executed
41-
print(f"Setting up test: {item.name}")
42-
43-
44-
def pytest_runtest_teardown(item: pytest.Item, nextitem: pytest.Item | None) -> None:
45-
# This hook is called after each test function is executed
46-
print(f"Tearing down test: {item.name}")

airbyte_cdk/test/declarative/test_suites/__init__.py renamed to airbyte_cdk/test/standard_tests/__init__.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,20 @@
44
Here we have base classes for a robust set of declarative connector test suites.
55
"""
66

7-
from airbyte_cdk.test.declarative.test_suites.connector_base import (
7+
from airbyte_cdk.test.standard_tests.connector_base import (
88
ConnectorTestScenario,
99
ConnectorTestSuiteBase,
10-
generate_tests,
1110
)
12-
from airbyte_cdk.test.declarative.test_suites.declarative_sources import (
11+
from airbyte_cdk.test.standard_tests.declarative_sources import (
1312
DeclarativeSourceTestSuite,
1413
)
15-
from airbyte_cdk.test.declarative.test_suites.destination_base import DestinationTestSuiteBase
16-
from airbyte_cdk.test.declarative.test_suites.source_base import SourceTestSuiteBase
14+
from airbyte_cdk.test.standard_tests.destination_base import DestinationTestSuiteBase
15+
from airbyte_cdk.test.standard_tests.source_base import SourceTestSuiteBase
1716

1817
__all__ = [
1918
"ConnectorTestScenario",
2019
"ConnectorTestSuiteBase",
2120
"DeclarativeSourceTestSuite",
2221
"DestinationTestSuiteBase",
2322
"SourceTestSuiteBase",
24-
"generate_tests",
2523
]

airbyte_cdk/test/declarative/test_suites/connector_base.py renamed to airbyte_cdk/test/standard_tests/connector_base.py

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -28,49 +28,6 @@
2828
MANIFEST_YAML = "manifest.yaml"
2929

3030

31-
def generate_tests(metafunc: pytest.Metafunc) -> None:
32-
"""
33-
A helper for pytest_generate_tests hook.
34-
35-
If a test method (in a class subclassed from our base class)
36-
declares an argument 'scenario', this function retrieves the
37-
'scenarios' attribute from the test class and parametrizes that
38-
test with the values from 'scenarios'.
39-
40-
## Usage
41-
42-
```python
43-
from airbyte_cdk.test.declarative.test_suites.connector_base import (
44-
generate_tests,
45-
ConnectorTestSuiteBase,
46-
)
47-
48-
def pytest_generate_tests(metafunc):
49-
generate_tests(metafunc)
50-
51-
class TestMyConnector(ConnectorTestSuiteBase):
52-
...
53-
54-
```
55-
"""
56-
# Check if the test function requires an 'scenario' argument
57-
if "scenario" in metafunc.fixturenames:
58-
# Retrieve the test class
59-
test_class = metafunc.cls
60-
if test_class is None:
61-
raise ValueError("Expected a class here.")
62-
# Get the 'scenarios' attribute from the class
63-
scenarios_attr = getattr(test_class, "get_scenarios", None)
64-
if scenarios_attr is None:
65-
raise ValueError(
66-
f"Test class {test_class} does not have a 'scenarios' attribute. "
67-
"Please define the 'scenarios' attribute in the test class."
68-
)
69-
70-
scenarios = test_class.get_scenarios()
71-
ids = [str(scenario) for scenario in scenarios]
72-
metafunc.parametrize("scenario", scenarios, ids=ids)
73-
7431

7532
class ConnectorTestSuiteBase(abc.ABC):
7633
"""Base class for connector test suites."""

airbyte_cdk/test/declarative/test_suites/declarative_sources.py renamed to airbyte_cdk/test/standard_tests/declarative_sources.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@
1010
ConcurrentDeclarativeSource,
1111
)
1212
from airbyte_cdk.test.declarative.models import ConnectorTestScenario
13-
from airbyte_cdk.test.declarative.test_suites.connector_base import MANIFEST_YAML
14-
from airbyte_cdk.test.declarative.test_suites.source_base import (
15-
SourceTestSuiteBase,
16-
)
1713
from airbyte_cdk.test.declarative.utils.job_runner import IConnector
14+
from airbyte_cdk.test.standard_tests.connector_base import MANIFEST_YAML
15+
from airbyte_cdk.test.standard_tests.source_base import SourceTestSuiteBase
1816

1917

2018
def md5_checksum(file_path: Path) -> str:

airbyte_cdk/test/declarative/test_suites/destination_base.py renamed to airbyte_cdk/test/standard_tests/destination_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright (c) 2024 Airbyte, Inc., all rights reserved.
22
"""Base class for destination test suites."""
33

4-
from airbyte_cdk.test.declarative.test_suites.connector_base import ConnectorTestSuiteBase
4+
from airbyte_cdk.test.standard_tests.connector_base import ConnectorTestSuiteBase
55

66

77
class DestinationTestSuiteBase(ConnectorTestSuiteBase):
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Copyright (c) 2025 Airbyte, Inc., all rights reserved.
2+
"""Pytest hooks for Airbyte CDK tests.
3+
4+
These hooks are used to customize the behavior of pytest during test discovery and execution.
5+
6+
To use these hooks within a connector, add the following lines to the connector's `conftest.py`
7+
file, or to another file that is imported during test discovery:
8+
9+
```python
10+
pytest_plugins = [
11+
"airbyte_cdk.test.standard_tests.pytest_hooks",
12+
]
13+
```
14+
"""
15+
import pytest
16+
17+
18+
def pytest_generate_tests(metafunc: pytest.Metafunc) -> None:
19+
"""
20+
A helper for pytest_generate_tests hook.
21+
22+
If a test method (in a class subclassed from our base class)
23+
declares an argument 'scenario', this function retrieves the
24+
'scenarios' attribute from the test class and parametrizes that
25+
test with the values from 'scenarios'.
26+
27+
## Usage
28+
29+
```python
30+
from airbyte_cdk.test.standard_tests.connector_base import (
31+
generate_tests,
32+
ConnectorTestSuiteBase,
33+
)
34+
35+
def pytest_generate_tests(metafunc):
36+
generate_tests(metafunc)
37+
38+
class TestMyConnector(ConnectorTestSuiteBase):
39+
...
40+
41+
```
42+
"""
43+
# Check if the test function requires an 'scenario' argument
44+
if "scenario" in metafunc.fixturenames:
45+
# Retrieve the test class
46+
test_class = metafunc.cls
47+
if test_class is None:
48+
return
49+
50+
# Get the 'scenarios' attribute from the class
51+
scenarios_attr = getattr(test_class, "get_scenarios", None)
52+
if scenarios_attr is None:
53+
raise ValueError(
54+
f"Test class {test_class} does not have a 'scenarios' attribute. "
55+
"Please define the 'scenarios' attribute in the test class."
56+
)
57+
58+
scenarios = test_class.get_scenarios()
59+
ids = [str(scenario) for scenario in scenarios]
60+
metafunc.parametrize("scenario", scenarios, ids=ids)

airbyte_cdk/test/declarative/test_suites/source_base.py renamed to airbyte_cdk/test/standard_tests/source_base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
from airbyte_cdk.test.declarative.models import (
1717
ConnectorTestScenario,
1818
)
19-
from airbyte_cdk.test.declarative.test_suites.connector_base import (
19+
from airbyte_cdk.test.declarative.utils.job_runner import run_test_job
20+
from airbyte_cdk.test.standard_tests.connector_base import (
2021
ConnectorTestSuiteBase,
2122
)
22-
from airbyte_cdk.test.declarative.utils.job_runner import run_test_job
2323

2424

2525
class SourceTestSuiteBase(ConnectorTestSuiteBase):

unit_tests/resources/source_pokeapi_w_components_py/components.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""A sample implementation of custom components that does nothing but will cause syncs to fail if missing."""
22

33
from collections.abc import Iterable, MutableMapping
4-
from typing import Any, Mapping
4+
from typing import Any
55

66
import requests
77

unit_tests/resources/source_pokeapi_w_components_py/integration_tests/test_airbyte_standards.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
# Copyright (c) 2025 Airbyte, Inc., all rights reserved.
22
"""FAST Airbyte Standard Tests for the source_pokeapi_w_components source."""
33

4-
from airbyte_cdk.test.declarative.test_suites import (
5-
DeclarativeSourceTestSuite,
6-
generate_tests,
7-
)
4+
from airbyte_cdk.test.standard_tests import DeclarativeSourceTestSuite
85

9-
10-
def pytest_generate_tests(metafunc) -> None:
11-
generate_tests(metafunc)
6+
pytest_plugins = [
7+
"airbyte_cdk.test.standard_tests.pytest_hooks",
8+
]
129

1310

1411
class TestSuiteSourcePokeAPI(DeclarativeSourceTestSuite):

unit_tests/source_declarative_manifest/test_source_declarative_w_custom_components.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
custom_code_execution_permitted,
3333
register_components_module_from_string,
3434
)
35-
from airbyte_cdk.test.declarative.test_suites.connector_base import MANIFEST_YAML
35+
from airbyte_cdk.test.standard_tests.connector_base import MANIFEST_YAML
3636

3737
SAMPLE_COMPONENTS_PY_TEXT = """
3838
def sample_function() -> str:

0 commit comments

Comments
 (0)