[英]pytest test for running jupyter notebook
我的任务是在.py文件中编写一个python代码(pytest测试),该文件运行jupyter notebook并断言输出为1。
我有运行笔记本的方法:
def _notebook_run(path):
args = ["jupyter", "nbconvert", "--execute", path]
a = subprocess.check_call(args)
return a
并进行测试:
def test_test():
a = _notebook_run('./file.ipynb')
assert a == 1
当我使用此测试运行它时,它返回0,但是,当我单独运行jupyter notebook时,它返回1却是预期的。
为什么输出不同?
我在运行笔记本的方法上是否犯了一些错误?
有人可以帮忙吗?
更新:正如我发现的那样,无论在笔记本中执行什么代码,它总是返回0。 有人知道如何使其工作吗?
我已经更改了方法并获得了所需的结果:
def test_file(self):
result = os.popen("jupyter nbconvert --to script --execute --stdout file.ipynb | python3").read()
assert result == '1\n'
我的错误原因是所使用的方法始终返回0,但不返回命令的输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.