Skip to content

Commit 34903b9

Browse files
committed
Test suite. TODO: Make it work.
1 parent 5f1d0a8 commit 34903b9

File tree

8 files changed

+54
-5
lines changed

8 files changed

+54
-5
lines changed

config.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1-
{}
1+
{
2+
"files": {
3+
"./umock.py": "",
4+
"https://raw.githubusercontent.com/ntoll/upytest/main/upytest.py": "",
5+
"./tests/__init__.py": "tests/__init__.py",
6+
"./tests/test_asyncmock.py": "tests/test_asyncmock.py",
7+
"./tests/test_mock.py": "tests/test_mock.py",
8+
"./tests/test_patch.py": "tests/test_patch.py"
9+
}
10+
}

index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<script type="module" src="https://pyscript.net/releases/2024.6.1/core.js"></script>
1616
</head>
1717
<body>
18-
<script type="mpy" src="./main.py" config="./config.json" terminal></script>
18+
<h1>MicroMock test suite</h1>
19+
<script type="mpy" src="./main.py" config="./config.json" terminal async></script>
1920
</body>
2021
</html>

main.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
print("Hello, world!")
1+
import upytest
2+
3+
4+
await upytest.run("./tests")

tests/__init__.py

Whitespace-only changes.

tests/test_asyncmock.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"""
2+
Ensure AsyncMock works as expected.
3+
"""

tests/test_mock.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
Tests to ensure that the Mock class works as expected (close to, but not
3+
exacty the same as unittest.mock.Mock).
4+
"""
5+
6+
from umock import Mock
7+
8+
9+
def test_init_mock():
10+
"""
11+
A Mock object should be created with no attributes.
12+
"""
13+
mock = Mock()
14+
assert mock.__dict__ == {}, "Not an empty dict."
15+
16+
def test_init_mock_with_spec():
17+
"""
18+
A Mock object should be created with the specified attributes.
19+
"""
20+
pass
21+
22+
def test_init_mock_with_spec_and_values():
23+
"""
24+
A Mock object should be created with the specified attributes and values.
25+
"""
26+
pass

tests/test_patch.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"""
2+
Tests the patch decorator/context manager.
3+
"""

umock.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ def __getattr__(self, name):
217217
"""
218218
Return a callable that records the call.
219219
"""
220+
if name.startswith("_"):
221+
return super().__getattr__(name)
220222
if hasattr(self, "return_value"):
221223
return self.return_value
222224
else:
@@ -226,17 +228,19 @@ def __setattr__(self, name, value):
226228
"""
227229
Set an attribute on the mock object.
228230
"""
231+
if name.startswith("_"):
232+
super().__setattr__(name, value)
229233
if hasattr(self, "_spec") and name not in self._spec:
230234
raise AttributeError(f"{name} is not in the mock's spec.")
231-
setattr(self, name, value)
235+
super().__setattr__(name, value)
232236

233237
def __delattr__(self, name):
234238
"""
235239
Delete an attribute on the mock object.
236240
"""
237241
if hasattr(self, "_spec") and name not in self._spec:
238242
raise AttributeError(f"{name} is not in the mock's spec.")
239-
delattr(self, name)
243+
super().__delattr__(name)
240244

241245

242246
class AsyncMock(Mock):

0 commit comments

Comments
 (0)