Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Gold custom logging tests #994

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

bobhancockg
Copy link
Contributor

No description provided.

This commit introduces a test suite for the files in the
`examples/custom_logging_interceptor` directory.

The following changes were made:
- I created a `tests` subdirectory within `examples/custom_logging_interceptor`.
- I added `tests/test_cloud_logging_interceptor.py` with unit tests for the
  `CloudLoggingInterceptor` class, covering its initialization, request logging
  (successful and failed), and RPC interception methods.
- I added `tests/test_get_campaigns.py` with tests for the `get_campaigns.py`
  script, including testing its main functionality with mocked API calls,
  error handling for GoogleAdsException, and command-line argument parsing.
- I added an empty `tests/__init__.py` to make the `tests` directory a
  Python package.
- I updated `examples/custom_logging_interceptor/requirements.txt` to include
  `pytest` and `pytest-mock` as testing dependencies.
This commit addresses test failures encountered during collection and execution
for the `examples/custom_logging_interceptor` test suite.

The following changes were made:
- Modified `test_cloud_logging_interceptor.py`:
    - Adjusted mocking strategy: Removed initial `sys.modules` patching for `google.cloud.logging`.
    - Relied on and refined the `mock_cloud_logging_client` fixture to correctly mock `google.cloud.logging.Client` and allow assertions on the logger instance.
- Modified `examples/custom_logging_interceptor/get_campaigns.py`:
    - Exposed the argparse parser instance to allow tests to access it directly without running the `if __name__ == '__main__'` block for parsing tests.
- Updated `examples/custom_logging_interceptor/requirements.txt`:
    - Added `PyYAML` and `google-ads` as explicit dependencies, as they were found to be necessary for the tests to run correctly and were implicitly required by the example code.
- Ensured test files use appropriate mocking for external services and client libraries, allowing tests to pass without actual calls to Google Cloud or Google Ads APIs.
This commit ensures that the test suite for the
`examples/custom_logging_interceptor` example passes reliably.

The primary issue causing `ImportError: cannot import name 'logging' from 'google.cloud'`
during test collection was related to the test environment not having all
necessary dependencies installed correctly prior to running pytest.

The resolution involved:
- Verifying and ensuring `google-cloud-logging` is listed in
  `examples/custom_logging_interceptor/requirements.txt`.
- Confirming that installing all dependencies listed in `requirements.txt`
  (including `google-cloud-logging`, `PyYAML`, `google-ads`, `pytest`,
  and `pytest-mock`) before running `pytest` resolves the collection
  and execution errors.
- Previous commits already included necessary refinements to the test files
  themselves and additions of `PyYAML` and `google-ads` to requirements.

The tests now pass when the environment is correctly prepared by installing
all specified dependencies.
@bobhancockg bobhancockg requested a review from a team as a code owner May 31, 2025 20:38
@bobhancockg bobhancockg requested review from BenRKarl and laurachevalier4 and removed request for laurachevalier4 May 31, 2025 20:38
@bobhancockg bobhancockg changed the title Gold add custom logging tests Gold custom logging tests Jun 2, 2025
@BenRKarl BenRKarl added the kokoro:run This label is required to run a kokoro presubmit if an external contributor sends a PR. label Jun 5, 2025
@kokoro-team kokoro-team removed the kokoro:run This label is required to run a kokoro presubmit if an external contributor sends a PR. label Jun 5, 2025
@BenRKarl BenRKarl added the kokoro:force-run This label is required to run a kokoro presubmit if an external contributor sends a PR. label Jun 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kokoro:force-run This label is required to run a kokoro presubmit if an external contributor sends a PR.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants