-
Notifications
You must be signed in to change notification settings - Fork 6
/
test_run.py
86 lines (72 loc) · 3.16 KB
/
test_run.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
# -*- coding: utf-8 -*-
import os
import io
import sys
import shutil
from . import nb_basic, run_args
# tests are run in subprocess because otherwise some state seems to be left
# around somewhere in jupyter (https://github.com/pyviz-dev/nbsmoke/issues/45)
# maybe this test is overkill now we check for certain output in the run tests?
def test_definitely_ran_paranoid(testdir):
assert not os.path.exists('sigh')
testdir.makefile('.ipynb', testing123=nb_basic%{'the_source':"open('x','w').write('y')"})
result = testdir.runpytest_subprocess(*run_args)
assert result.ret==0
with open('x','r') as f:
assert f.read() == 'y'
assert os.path.isfile('sigh')
def test_run_good(testdir):
testdir.makefile('.ipynb', testing123=nb_basic%{'the_source':"1/1"})
result = testdir.runpytest_subprocess(*run_args)
assert result.ret == 0
result.stdout.re_match_lines_random(
[".*collected 1 item$",
".*testing123.ipynb.*PASSED.*"])
def test_run_bad(testdir):
testdir.makefile('.ipynb', testing123=nb_basic%{'the_source':"1/0"})
result = testdir.runpytest_subprocess(*run_args)
assert result.ret == 1
result.stdout.re_match_lines_random([".*ZeroDivisionError.*"])
def test_run_good_html(testdir):
outhtml = os.path.join(testdir.tmpdir.strpath,'testing123.ipynb.html')
assert not os.path.exists(outhtml)
testdir.makefile('.ipynb', testing123=nb_basic%{'the_source':"42"})
result = testdir.runpytest_subprocess(*(run_args+['--store-html=%s'%testdir.tmpdir.strpath]))
assert result.ret == 0
# test that html has happened
targets = [
"<pre>42</pre>",
# note: this is really what happens in a python2 notebook
"<pre>'中国'</pre>" if sys.version_info[0]==3 else r"<pre>u'\u4e2d\u56fd'</pre>"]
answer = [None,None]
with io.open(outhtml,encoding='utf8') as f:
for line in f:
for i,target in enumerate(targets):
if target in line:
answer[i] = 42
assert answer == [42,42]
def test_skip_run(testdir):
testdir.makeini(r"""
[pytest]
nbsmoke_skip_run = ^.*skipme\.ipynb$
^.*skipmetoo.*$
""")
testdir.makefile('.ipynb', skipme=nb_basic%{'the_source':"1/0"})
testdir.makefile('.ipynb', alsoskipme=nb_basic%{'the_source':"1/0"})
testdir.makefile('.ipynb', skipmetoo=nb_basic%{'the_source':"1/0"})
testdir.makefile('.ipynb', skipmenot=nb_basic%{'the_source':"1/1"})
result = testdir.runpytest_subprocess(*run_args)
assert result.ret == 0
result.stdout.re_match_lines_random(
[".*collected 4 items$",
".*alsoskipme.ipynb.*SKIPPED",
".*skipme.ipynb.*SKIPPED",
".*skipmenot.ipynb.*PASSED",
".*skipmetoo.ipynb.*SKIPPED"])
def test_cwd_like_jupyter_notebook(testdir):
p = testdir.tmpdir.mkdir("sub").join("hello.txt")
p.write("content")
testdir.makefile('.ipynb', testing123=nb_basic%{'the_source':"import os; assert os.path.isfile('hello.txt')"})
shutil.move('testing123.ipynb', 'sub')
result = testdir.runpytest_subprocess(*run_args)
assert result.ret == 0