No such file or directory处理

No such file or directory处理
问题描述

目录结构

    |   index.html
    |   test_login.py
    |   data
        |       login.yml
        |       parseryml.py

文件内容

#file:test_login.py
import pytest
import requests
from data.parseryml import td
###数据驱动,ddt:1.装饰器是哪一种 2.unittest框架是哪一个

param = {'X-Requested-With': 'XMLHttpRequest'}
@pytest.mark.parametrize('a',td())
def test_login(login,a):
    url ='http://www.shopxo.com/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_code
    assert excepted['message'] == res.json()['msg']
if __name__ == '__main__':
    pytest.main(['test_login.py'])
#file: parseryml.py
import yaml

def td():
    with open('login.yml', encoding='utf-8') as f:
        hf = yaml.load(f,Loader=yaml.FullLoader)
    return hf
if __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 td
    with 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中也可

最好的方法就是都统一使用绝对路径

  1. sys.path会查找python解释器的目录以及文件所在目录 ↩︎

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值