Skip to content

Commit ea1f06d

Browse files
committed
build pytest suite out of test cases
1 parent 795e771 commit ea1f06d

23 files changed

+993
-106
lines changed

pyproject.toml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ classifiers = [
2020
]
2121
requires-python = ">=3.9"
2222
dependencies = [
23-
"psutil>=0.0"
23+
"psutil>=0.0",
2424
]
2525

2626
[dependency-groups]
2727
dev = [
28-
"hatch>=1.14.1"
29-
]
28+
"hatch>=1.14.1",
29+
"pytest>=8.4.1",
30+
"pytest-asyncio>=1.0.0",
31+
]
3032

3133
[project.scripts]
3234
mprof = "memory_profiler.mprof:main"
@@ -58,3 +60,6 @@ source = "vcs"
5860

5961
[tool.codespell]
6062
skip = './.git'
63+
64+
[tool.pytest.ini_options]
65+
testpaths = ["test"]

src/memory_profiler/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from .__main__ import (profile, memory_usage, __version__, LineProfiler, show_results)
2+
3+
__all__ = [
4+
'profile',
5+
'memory_usage',
6+
'__version__',
7+
'LineProfiler',
8+
'show_results'
9+
]

test/test_as.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import math
2+
from memory_profiler import profile
23

34
@profile
45
def f():
56
o = math.sqrt(2013)
67
return o
78

8-
if __name__ == '__main__':
9+
def test_f():
910
f()

test/test_async.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import asyncio
2-
import sys
2+
3+
import pytest
34

45
from memory_profiler import profile
56

@@ -10,18 +11,13 @@ async def my_func():
1011
b = [2] * (2 * 10 ** 7)
1112
await asyncio.sleep(1e-2)
1213
del b
14+
return 1, 2
1315

14-
async def main():
16+
@pytest.mark.asyncio
17+
async def test_async_func():
1518
task = asyncio.create_task(my_func())
1619
res = await asyncio.gather(task)
20+
assert res, "function didn't return anything"
21+
assert len(res) > 0, "task didn't return anything"
22+
assert res == [(1, 2), ]
1723

18-
async def main_legacy():
19-
future = asyncio.ensure_future(my_func())
20-
res = await asyncio.gather(future)
21-
22-
if __name__ == '__main__':
23-
if sys.version_info >= (3, 7):
24-
asyncio.run(main()) # main loop
25-
else:
26-
loop = asyncio.get_event_loop()
27-
loop.run_until_complete(main_legacy())

test/test_attributes.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33

44
@profile
5-
def test_with_profile(arg1):
5+
def func_with_profile(arg1):
66
"""dummy doc"""
77
return None
88

99

10-
if __name__ == '__main__':
11-
assert test_with_profile.__doc__ == "dummy doc"
12-
assert test_with_profile.__name__ == "test_with_profile"
10+
def test_with_profile():
11+
assert func_with_profile.__doc__ == "dummy doc"
12+
assert func_with_profile.__name__ == "func_with_profile"

test/test_exception.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1+
import pytest
12

23
# make sure that memory profiler does not hang on exception
34
from memory_profiler import memory_usage
45

56
def foo():
67
raise NotImplementedError('Error')
78

8-
try:
9-
out = memory_usage((foo, tuple(), {}), timeout=1)
10-
except NotImplementedError:
11-
pass
12-
print('Success')
9+
def test_hang_on_exception():
10+
11+
with pytest.raises(NotImplementedError):
12+
out = memory_usage((foo, tuple(), {}), timeout=1)

test/test_exit_code.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class TestExitCode(unittest.TestCase):
88
def setUp(self):
99
# to be able to import mprof
1010
sys.path.append('.')
11-
from mprof import run_action
11+
from memory_profiler.mprof import run_action
1212
self.run_action = run_action
1313

1414
def test_exit_code_success(self):

test/test_func.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
from memory_profiler import profile
12

23
@profile
3-
def test_1(i):
4+
def func_1(i):
45
# .. will be called twice ..
56
c = {}
67
for i in range(i):
78
c[i] = 2
89

9-
if __name__ == '__main__':
10-
test_1(10)
11-
test_1(10000)
10+
def test_func():
11+
12+
func_1(10)
13+
func_1(10000)

test/test_gen.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from memory_profiler import profile
12

23
@profile
34
def my_func():
@@ -8,7 +9,7 @@ def my_func():
89

910

1011
@profile
11-
def test_comprehension():
12+
def func_comprehension():
1213
# Dict comprehension
1314
d_comp = dict((str(k*k), [v] * (1<<17))
1415
for (v, k) in enumerate(range(99, 111)))
@@ -33,7 +34,7 @@ def hh(x=1):
3334

3435

3536
@profile
36-
def test_generator():
37+
def func_generator():
3738
a_gen = ([42] * (1<<20) for __ in '123')
3839
huge_lst = list(a_gen)
3940

@@ -43,10 +44,14 @@ def test_generator():
4344

4445
return a_gen
4546

47+
def test_gen():
48+
49+
# with profile:
50+
next(my_func()) # Issue #42
51+
52+
def test_generator():
53+
_ = func_generator()
4654

47-
if __name__ == '__main__':
48-
with profile:
49-
next(my_func()) # Issue #42
5055

51-
test_generator()
52-
test_comprehension()
56+
def test_comprehension():
57+
_ = func_comprehension()

test/test_import.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ def my_func():
88
del b
99
return a
1010

11-
if __name__ == '__main__':
11+
def test_my_func():
1212
my_func()

0 commit comments

Comments
 (0)