forked from wradlib/wradlib-notebooks
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconftest.py
57 lines (41 loc) · 1.57 KB
/
conftest.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
#!/usr/bin/env python
# Copyright (c) 2019, wradlib developers.
# Distributed under the MIT License. See LICENSE.txt for more info.
import pytest
import sys
import io
import os
import nbformat
from nbconvert.preprocessors import ExecutePreprocessor
from nbconvert.preprocessors.execute import CellExecutionError
def pytest_collect_file(path, parent):
if path.ext == ".ipynb":
return NotebookFile(path, parent)
class NotebookFile(pytest.File):
def collect(self):
for f in [self.fspath]:
yield NotebookItem(os.path.basename(f), self)
def setup(self):
kernel = 'python%d' % sys.version_info[0]
self.exproc = ExecutePreprocessor(kernel_name=kernel, timeout=600)
class NotebookItem(pytest.Item):
def __init__(self, name, parent):
super(NotebookItem, self).__init__(name, parent)
def runtest(self):
cur_dir = os.path.dirname(self.fspath)
with self.fspath.open() as f:
nb = nbformat.read(f, as_version=4)
try:
self.parent.exproc.preprocess(nb,
{'metadata': {'path': cur_dir}})
except CellExecutionError as e:
raise NotebookException(e)
with io.open(self.fspath, 'wt') as f:
nbformat.write(nb, f)
def repr_failure(self, excinfo):
if isinstance(excinfo.value, NotebookException):
return f"{excinfo.value}\n{excinfo.traceback}"
def reportinfo(self):
return self.fspath, 0, "TestCase: %s" % self.name
class NotebookException(Exception):
pass