forked from oppia/oppia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest_test.py
More file actions
134 lines (109 loc) · 5.1 KB
/
conftest_test.py
File metadata and controls
134 lines (109 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# coding: utf-8
#
# Copyright 2025 The Oppia Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS-IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for conftest.py pytest configuration."""
from __future__ import annotations
import os
import sys
import unittest
from unittest import mock
import conftest
from scripts import common
class PytestConfigureHookTests(unittest.TestCase):
"""Tests for pytest_configure hook."""
def test_pytest_configure_adds_missing_paths_to_sys_path(self) -> None:
"""Test that pytest_configure adds directories that aren't in sys.path."""
# Create a mock config object.
mock_config = mock.MagicMock()
# Save the original sys.path.
original_sys_path = sys.path.copy()
try:
# Remove one of the required directories from sys.path to test
# that pytest_configure adds it back.
test_dir = common.DIRS_TO_ADD_TO_SYS_PATH[0]
assert os.path.exists(test_dir)
# Ensure that that directory is not in sys.path.
if test_dir in sys.path:
sys.path.remove(test_dir)
assert test_dir not in sys.path
# Call pytest_configure and verify the directory was added.
conftest.pytest_configure(mock_config)
assert test_dir in sys.path
finally:
# Restore original sys.path.
sys.path[:] = original_sys_path
def test_pytest_configure_does_not_duplicate_paths(self) -> None:
"""Test that pytest_configure doesn't add duplicate paths."""
# Create a mock config object.
mock_config = mock.MagicMock()
# Save the original sys.path.
original_sys_path = sys.path.copy()
try:
# Ensure all paths are already in sys.path.
for directory in common.DIRS_TO_ADD_TO_SYS_PATH:
assert os.path.exists(directory)
if directory not in sys.path:
sys.path.insert(0, directory)
# Count how many times each directory appears.
path_counts = {
d: sys.path.count(d) for d in common.DIRS_TO_ADD_TO_SYS_PATH
}
# Call pytest_configure.
conftest.pytest_configure(mock_config)
# Verify no duplicates were added.
for directory in common.DIRS_TO_ADD_TO_SYS_PATH:
assert (
sys.path.count(directory) == path_counts[directory]
), f'Directory {directory} was duplicated in sys.path'
finally:
# Restore original sys.path.
sys.path[:] = original_sys_path
def test_pytest_configure_adds_all_required_paths(self) -> None:
"""Test that pytest_configure adds all paths from DIRS_TO_ADD_TO_SYS_PATH."""
# Create a mock config object.
mock_config = mock.MagicMock()
# Call pytest_configure (it should have already been called by pytest,
# but calling it again should be safe).
conftest.pytest_configure(mock_config)
# Verify all existing directories are in sys.path.
for directory in common.DIRS_TO_ADD_TO_SYS_PATH:
assert os.path.exists(directory)
assert (
directory in sys.path
), f'Required directory {directory} not found in sys.path'
class EnvironmentSetupTests(unittest.TestCase):
"""Tests for environment variable setup in conftest.py."""
def test_environment_variables_are_set(self) -> None:
"""Test that required environment variables are set at module load time."""
# These environment variables should be set when conftest.py is imported.
expected_vars = {
'DATASTORE_DATASET': 'dev-project-id',
'DATASTORE_EMULATOR_HOST': 'localhost:8089',
'DATASTORE_PROJECT_ID': 'dev-project-id',
'GOOGLE_CLOUD_PROJECT': 'dev-project-id',
'APPLICATION_ID': 'dev-project-id',
}
for var, expected_value in expected_vars.items():
assert var in os.environ, f'Environment variable {var} not set'
assert (
os.environ[var] == expected_value
), f'Environment variable {var} has wrong value'
def test_curr_dir_added_to_sys_path(self) -> None:
"""Test that CURR_DIR is added to sys.path at module load time."""
# When conftest.py is imported, it should add CURR_DIR to sys.path.
# By the time this test runs, CURR_DIR should already be in sys.path
# because conftest.py was imported at test collection time.
curr_dir = os.path.abspath(os.getcwd())
assert curr_dir in sys.path, 'CURR_DIR not found in sys.path'