Skip to content

Commit 288efbf

Browse files
authored
Cleanup test classes to eliminate warnings coming from PyTest. (#168)
* Cleanup test classes to eliminate warnings coming from PyTest. - Introduced `MockTestingAdapter` and `MockTestingCustomState` for unit testing. - Replaced instances of `TestingAdapter` with `MockTestingAdapter` in various test files. - Updated `test_agent_state.py` to utilize `_MockTestDataItem` instead of `TestDataItem`. - Added `ObsoleteTestClient` for potential future testing scenarios. - Refactored `test_turn_context.py` to use `_SimpleTestingAdapter` for context testing. - Ensured all tests are aligned with the new mock implementations for better isolation and control. * Refactor test data organization and update configurations for consistency across tests Enable strict "warnings as errors" in PyTest. Fix associated issues and errors that came up. * Fix import statement to use DEFAULT_TEST_VALUES for consistency in test setup
1 parent c53ea26 commit 288efbf

33 files changed

+227
-154
lines changed

libraries/microsoft-agents-storage-cosmos/microsoft_agents/storage/cosmos/cosmos_db_storage_config.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ def __init__(
4242
"""
4343
config_file: str = kwargs.get("filename", "")
4444
if config_file:
45-
kwargs = json.load(open(config_file))
45+
with open(config_file) as f:
46+
kwargs = json.load(f)
4647
self.cosmos_db_endpoint: str = cosmos_db_endpoint or kwargs.get(
4748
"cosmos_db_endpoint", ""
4849
)

pytest.ini

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
[pytest]
2+
# Pytest configuration for Microsoft Agents for Python
3+
4+
# Treat all warnings as errors by default
5+
# This ensures that any code generating warnings will fail tests,
6+
# promoting cleaner code and early detection of issues
7+
filterwarnings =
8+
error
9+
# Ignore specific warnings that are not actionable or are from dependencies
10+
ignore::DeprecationWarning:pkg_resources.*
11+
ignore::DeprecationWarning:setuptools.*
12+
ignore::PendingDeprecationWarning
13+
# pytest-asyncio warnings that are safe to ignore
14+
ignore:.*deprecated.*asyncio.*:DeprecationWarning:pytest_asyncio.*
15+
16+
# Test discovery configuration
17+
testpaths = tests
18+
python_files = test_*.py *_test.py
19+
python_classes = Test*
20+
python_functions = test_*
21+
22+
# Output configuration
23+
addopts =
24+
--strict-markers
25+
--strict-config
26+
--verbose
27+
--tb=short
28+
--durations=10
29+
30+
# Minimum version requirement
31+
minversion = 6.0
32+
33+
# Markers for test categorization
34+
markers =
35+
unit: Unit tests
36+
integration: Integration tests
37+
slow: Slow tests that may take longer to run
38+
requires_network: Tests that require network access
39+
requires_auth: Tests that require authentication

tests/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,27 @@
22

33
This document serves as a quick guide to the utilities we provide internally to test this SDK. More information will come with work on integration testing.
44

5+
## Pytest Configuration
6+
7+
The project uses a `pytest.ini` configuration file in the root directory that sets up the following testing standards:
8+
9+
- **Warnings as Errors**: All warnings are treated as errors (`filterwarnings = error`) to ensure clean code and early detection of issues
10+
- **Strict Configuration**: Uses `--strict-markers` and `--strict-config` to catch configuration issues
11+
- **Test Discovery**: Automatically discovers tests in the `tests/` directory following standard naming conventions
12+
- **Verbose Output**: Shows detailed test results with short tracebacks and duration information
13+
14+
To run tests locally:
15+
```bash
16+
# Run all tests
17+
python -m pytest
18+
19+
# Run tests for a specific module
20+
python -m pytest tests/activity/
21+
22+
# Run with custom options (these will be added to the configured defaults)
23+
python -m pytest -x --lf # Stop on first failure, run last failed tests
24+
```
25+
526
## Storage Tests
627

728
More info soon. For now, there are flags defined in the code that dictate whether the Cosmos DB and the Blob storage tests are run, as these tests rely on local emulators.

tests/_common/data/__init__.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
from .test_defaults import TEST_DEFAULTS
2-
from .test_auth_data import (
3-
TEST_AUTH_DATA,
1+
from .default_test_values import DEFAULT_TEST_VALUES
2+
from .auth_test_data import (
3+
AUTH_TEST_DATA,
44
create_test_auth_handler,
55
)
6-
from .test_storage_data import TEST_STORAGE_DATA
7-
from .test_flow_data import TEST_FLOW_DATA
8-
from .configs import TEST_ENV_DICT, TEST_ENV
9-
from .configs import TEST_AGENTIC_ENV_DICT, TEST_AGENTIC_ENV
6+
from .storage_test_data import STORAGE_TEST_DATA
7+
from .flow_test_data import FLOW_TEST_DATA
8+
from .configs import NON_AGENTIC_TEST_ENV_DICT, NON_AGENTIC_TEST_ENV
9+
from .configs import AGENTIC_TEST_ENV_DICT, AGENTIC_TEST_ENV
1010

1111
__all__ = [
12-
"TEST_DEFAULTS",
13-
"TEST_AUTH_DATA",
14-
"TEST_STORAGE_DATA",
15-
"TEST_FLOW_DATA",
12+
"DEFAULT_TEST_VALUES",
13+
"AUTH_TEST_DATA",
14+
"STORAGE_TEST_DATA",
15+
"FLOW_TEST_DATA",
1616
"create_test_auth_handler",
17-
"TEST_ENV_DICT",
18-
"TEST_ENV",
19-
"TEST_AGENTIC_ENV_DICT",
20-
"TEST_AGENTIC_ENV",
17+
"NON_AGENTIC_TEST_ENV_DICT",
18+
"NON_AGENTIC_TEST_ENV",
19+
"AGENTIC_TEST_ENV_DICT",
20+
"AGENTIC_TEST_ENV",
2121
]

tests/_common/data/test_auth_data.py renamed to tests/_common/data/auth_test_data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def create_test_auth_handler(
2929
)
3030

3131

32-
class TEST_AUTH_DATA:
32+
class AUTH_TEST_DATA:
3333
def __init__(self):
3434

3535
self.auth_handler: AuthHandler = create_test_auth_handler("graph")
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
from .test_auth_config import TEST_ENV_DICT, TEST_ENV
2-
from .test_agentic_auth_config import TEST_AGENTIC_ENV_DICT, TEST_AGENTIC_ENV
1+
from .test_auth_config import NON_AGENTIC_TEST_ENV_DICT, NON_AGENTIC_TEST_ENV
2+
from .test_agentic_auth_config import AGENTIC_TEST_ENV_DICT, AGENTIC_TEST_ENV
33

4-
__all__ = ["TEST_ENV_DICT", "TEST_ENV", "TEST_AGENTIC_ENV_DICT", "TEST_AGENTIC_ENV"]
4+
__all__ = [
5+
"NON_AGENTIC_TEST_ENV_DICT",
6+
"NON_AGENTIC_TEST_ENV",
7+
"AGENTIC_TEST_ENV_DICT",
8+
"AGENTIC_TEST_ENV",
9+
]

tests/_common/data/configs/test_agentic_auth_config.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from microsoft_agents.activity import load_configuration_from_env
22

33
from ...create_env_var_dict import create_env_var_dict
4-
from ..test_defaults import TEST_DEFAULTS
4+
from ..default_test_values import DEFAULT_TEST_VALUES
55

6-
DEFAULTS = TEST_DEFAULTS()
6+
DEFAULTS = DEFAULT_TEST_VALUES()
77

88
_TEST_AGENTIC_ENV_RAW = """
99
CONNECTIONS__SERVICE_CONNECTION__SETTINGS__TENANTID=service-tenant-id
@@ -46,9 +46,9 @@
4646
)
4747

4848

49-
def TEST_AGENTIC_ENV():
49+
def AGENTIC_TEST_ENV():
5050
return create_env_var_dict(_TEST_AGENTIC_ENV_RAW)
5151

5252

53-
def TEST_AGENTIC_ENV_DICT():
54-
return load_configuration_from_env(TEST_AGENTIC_ENV())
53+
def AGENTIC_TEST_ENV_DICT():
54+
return load_configuration_from_env(AGENTIC_TEST_ENV())

tests/_common/data/configs/test_auth_config.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from microsoft_agents.activity import load_configuration_from_env
22

33
from ...create_env_var_dict import create_env_var_dict
4-
from ..test_defaults import TEST_DEFAULTS
4+
from ..default_test_values import DEFAULT_TEST_VALUES
55

6-
DEFAULTS = TEST_DEFAULTS()
6+
DEFAULTS = DEFAULT_TEST_VALUES()
77

88
_TEST_ENV_RAW = """
99
AGENTAPPLICATION__USERAUTHORIZATION__HANDLERS__{auth_handler_id}__SETTINGS__AZUREBOTOAUTHCONNECTIONNAME={abs_oauth_connection_name}
@@ -20,9 +20,9 @@
2020
)
2121

2222

23-
def TEST_ENV():
23+
def NON_AGENTIC_TEST_ENV():
2424
return create_env_var_dict(_TEST_ENV_RAW)
2525

2626

27-
def TEST_ENV_DICT():
28-
return load_configuration_from_env(TEST_ENV())
27+
def NON_AGENTIC_TEST_ENV_DICT():
28+
return load_configuration_from_env(NON_AGENTIC_TEST_ENV())

tests/_common/data/test_defaults.py renamed to tests/_common/data/default_test_values.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
)
77

88

9-
class TEST_DEFAULTS:
9+
class DEFAULT_TEST_VALUES:
1010
def __init__(self):
1111

1212
self.token = "__token"

tests/_common/data/test_flow_data.py renamed to tests/_common/data/flow_test_data.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
from microsoft_agents.hosting.core._oauth import _FlowState, _FlowStateTag
44

55
from tests._common.storage import MockStoreItem
6-
from tests._common.data.test_defaults import TEST_DEFAULTS
6+
from tests._common.data.default_test_values import DEFAULT_TEST_VALUES
77

8-
DEFAULTS = TEST_DEFAULTS()
8+
DEFAULTS = DEFAULT_TEST_VALUES()
99

1010
DEF_FLOW_ARGS = {
1111
"ms_app_id": DEFAULTS.ms_app_id,
@@ -15,7 +15,7 @@
1515
}
1616

1717

18-
class TEST_FLOW_DATA:
18+
class FLOW_TEST_DATA:
1919
def __init__(self):
2020

2121
self.not_started = _FlowState(

0 commit comments

Comments
 (0)