forked from joerick/pyinstrument
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_ipython_magic.py
94 lines (63 loc) · 2.28 KB
/
test_ipython_magic.py
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
from test.fake_time_util import fake_time
import pytest
# note: IPython should be imported within each test. Importing it in our tests
# seems to cause problems with subsequent tests.
cell_code = """
import time
def function_a():
function_b()
function_c()
def function_b():
function_d()
def function_c():
function_d()
def function_d():
function_e()
def function_e():
time.sleep(0.1)
function_a()
"""
# Tests #
@pytest.mark.ipythonmagic
def test_magics(ip):
from IPython.utils.capture import capture_output as capture_ipython_output
with fake_time():
with capture_ipython_output() as captured:
ip.run_cell_magic("pyinstrument", line="", cell=cell_code)
assert len(captured.outputs) == 1
output = captured.outputs[0]
assert "text/html" in output.data
assert "text/plain" in output.data
assert "function_a" in output.data["text/html"]
assert "<iframe" in output.data["text/html"]
assert "function_a" in output.data["text/plain"]
assert "- 0.200 function_a" in output.data["text/plain"]
assert "- 0.100 FakeClock.sleep" in output.data["text/plain"]
with fake_time():
with capture_ipython_output() as captured:
# this works because function_a was defined in the previous cell
ip.run_line_magic("pyinstrument", line="function_a()")
assert len(captured.outputs) == 1
output = captured.outputs[0]
assert "function_a" in output.data["text/plain"]
assert "- 0.100 FakeClock.sleep" in output.data["text/plain"]
@pytest.mark.ipythonmagic
def test_magic_empty_line(ip):
# check empty line input
ip.run_line_magic("pyinstrument", line="")
@pytest.mark.ipythonmagic
def test_magic_no_variable_expansion(ip, capsys):
ip.run_line_magic("pyinstrument", line="print(\"hello {len('world')}\")")
captured = capsys.readouterr()
assert "hello {len('world')}" in captured.out
assert "hello 5" not in captured.out
# Utils #
@pytest.fixture(scope="module")
def session_ip():
from IPython.testing.globalipapp import start_ipython
yield start_ipython()
@pytest.fixture(scope="function")
def ip(session_ip):
session_ip.run_line_magic(magic_name="load_ext", line="pyinstrument")
yield session_ip
session_ip.run_line_magic(magic_name="reset", line="-f")