如何使用pyfakefs运行lxml?
import os
import unittest
from lxml import etree
from pyfakefs import fake_filesystem_unittest
class TestExample(fake_filesystem_unittest.TestCase):
def setUp(self):
self.setUpPyfakefs()
def test_lxml(self):
os.mkdir('/test')
root = etree.Element("root")
tree = etree.ElementTree(root)
tree.write('/test/file.xml')
if __name__ == "__main__":
unittest.main()
运行上面的示例时,我收到此错误:
Traceback (most recent call last):
File "example_test.py", line 25, in test_lxml
File "src/lxml/lxml.etree.pyx", line 2033, in lxml.etree._ElementTree.write (src/lxml/lx
ml.etree.c:63707)
File "src/lxml/serializer.pxi", line 512, in lxml.etree._tofilelike (src/lxml/lxml.etree
.c:134950)
File "src/lxml/serializer.pxi", line 571, in lxml.etree._create_output_buffer (src/lxml/
lxml.etree.c:135614)
File "src/lxml/serializer.pxi", line 560, in lxml.etree._create_output_buffer (src/lxml/
lxml.etree.c:135415)
IOError: [Errno 2] No such file or directory
我在 macOS 10.12.4 上的 Python 2.7.13 中使用 lxml 3.6.4 和 pyfakefs 3.1