Skip to content

Commit a62e2a1

Browse files
Fix ModuleNotFoundError in advanced_operations tests
The tests in `examples/advanced_operations/tests/` were failing with `ModuleNotFoundError: No module named 'examples'` because the project root directory was not in Python's `sys.path` during test execution. This commit fixes the issue by adding a code snippet at the beginning of each affected test file. This snippet calculates the path to the project root (three levels above the test file's directory) and inserts it into `sys.path`. This ensures that the `examples` package can be correctly imported regardless of the working directory from which pytest is invoked. All tests in `examples/advanced_operations/tests/` now pass after this change.
1 parent fe65a76 commit a62e2a1

8 files changed

+72
-0
lines changed

examples/advanced_operations/tests/test_add_demand_gen_campaign.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
import sys
2+
import os
3+
# Calculate the path to the project root directory
4+
# __file__ is examples/advanced_operations/tests/test_name.py
5+
# project_root is three levels up from the test file's directory
6+
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", ".."))
7+
if project_root not in sys.path:
8+
sys.path.insert(0, project_root)
9+
110
import unittest
211
from unittest import mock
312
import sys

examples/advanced_operations/tests/test_add_dynamic_page_feed_asset.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
import sys
2+
import os
3+
# Calculate the path to the project root directory
4+
# __file__ is examples/advanced_operations/tests/test_name.py
5+
# project_root is three levels up from the test file's directory
6+
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", ".."))
7+
if project_root not in sys.path:
8+
sys.path.insert(0, project_root)
9+
110
import unittest
211
from unittest import mock
312
import sys

examples/advanced_operations/tests/test_add_smart_campaign.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
import sys
2+
import os
3+
# Calculate the path to the project root directory
4+
# __file__ is examples/advanced_operations/tests/test_name.py
5+
# project_root is three levels up from the test file's directory
6+
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", ".."))
7+
if project_root not in sys.path:
8+
sys.path.insert(0, project_root)
9+
110
import unittest
211
from unittest import mock
312
import sys

examples/advanced_operations/tests/test_create_and_attach_shared_keyword_set.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
import sys
2+
import os
3+
# Calculate the path to the project root directory
4+
# __file__ is examples/advanced_operations/tests/test_name.py
5+
# project_root is three levels up from the test file's directory
6+
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", ".."))
7+
if project_root not in sys.path:
8+
sys.path.insert(0, project_root)
9+
110
import unittest
211
from unittest import mock
312
import sys

examples/advanced_operations/tests/test_find_and_remove_criteria_from_shared_set.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
import sys
2+
import os
3+
# Calculate the path to the project root directory
4+
# __file__ is examples/advanced_operations/tests/test_name.py
5+
# project_root is three levels up from the test file's directory
6+
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", ".."))
7+
if project_root not in sys.path:
8+
sys.path.insert(0, project_root)
9+
110
import unittest
211
from unittest import mock
312
import sys

examples/advanced_operations/tests/test_get_ad_group_bid_modifiers.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
import sys
2+
import os
3+
# Calculate the path to the project root directory
4+
# __file__ is examples/advanced_operations/tests/test_name.py
5+
# project_root is three levels up from the test file's directory
6+
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", ".."))
7+
if project_root not in sys.path:
8+
sys.path.insert(0, project_root)
9+
110
import unittest
211
from unittest import mock
312
import sys

examples/advanced_operations/tests/test_use_cross_account_bidding_strategy.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
import sys
2+
import os
3+
# Calculate the path to the project root directory
4+
# __file__ is examples/advanced_operations/tests/test_name.py
5+
# project_root is three levels up from the test file's directory
6+
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", ".."))
7+
if project_root not in sys.path:
8+
sys.path.insert(0, project_root)
9+
110
import unittest
211
from unittest import mock
312
import sys

examples/advanced_operations/tests/test_use_portfolio_bidding_strategy.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
import sys
2+
import os
3+
# Calculate the path to the project root directory
4+
# __file__ is examples/advanced_operations/tests/test_name.py
5+
# project_root is three levels up from the test file's directory
6+
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", ".."))
7+
if project_root not in sys.path:
8+
sys.path.insert(0, project_root)
9+
110
import unittest
211
from unittest import mock
312
import sys

0 commit comments

Comments
 (0)