文章目录
No such file or directory处理问题描述解决方法No such file or directory处理
问题描述
目录结构
| index.html| test_login.py| data| login.yml| parseryml.py
文件内容
#file:test_login.pyimport pytestimport requestsfrom data.parseryml import td###数据驱动,ddt:1.装饰器是哪一种 2.unittest框架是哪一个param = {'X-Requested-With': 'XMLHttpRequest'}@pytest.mark.parametrize('a',td())def test_login(login,a):url ='/shopxo/index.php?s=/index/user/login.html'data = a['data']excepted =a['excepted']param.update({'Cookie':login})res = requests.post(url=url,headers=param,data=data)print ("响应文本::",res.text)print ("a的值::",a)assert excepted['code']==res.status_codeassert excepted['message'] == res.json()['msg']if __name__ == '__main__':pytest.main(['test_login.py'])
#file: parseryml.pyimport yamldef td():with open('login.yml', encoding='utf-8') as f:hf = yaml.load(f,Loader=yaml.FullLoader)return hfif __name__ == '__main__':print (td())
报错描述
test_login.py:None (test_login.py)test_login.py:7: in <module>@pytest.mark.parametrize('a',td())data\parseryml.py:4: in tdwith open('login.yml', encoding='utf-8') as f:E FileNotFoundError: [Errno 2] No such file or directory: 'login.yml'
解决方法
出现问题的原因
运行test_login.py时会从sys.path1中查找路径,参数化中调用的td方法,传入的文件不在查找的路径中,所以报错No such file or directory
解决方法
方法1:所有的内容使用绝对路径导入的方式即可
方法2:将td方法解析的文件所在目录加入到sys.path中也可
sys.path会查找python解释器的目录以及文件所在目录 ↩︎