unittest discover 无法找到测试文件,因为一个模块内全局变量?? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
JasperHale

unittest discover 无法找到测试文件,因为一个模块内全局变量??

  •  
  •   JasperHale
    jasper-1024 Jul 4, 2021 969 views
    This topic created in 1760 days ago, the information mentioned may be changed or developed.

    楼主非专业 python 开发,今天遇到个完全没头脑的问题,请赐教!.

    • 今天 unittest 单元测试一直找不到测试文件,一行一行排查.
    • 结果居然是使用 geoip2.database 模块内一个全局变量 read = geoip2.database.Reader(geoipfile)
    • 只要有这个全局变量,测试文件就找不到,geoipfile 没影响...这是咋回事啊??...

    test_ip.py

    import unittest from .geoip import location class Test_lib(unittest.TestCase): def test_locations(self): ips = ['23.250.80.1', '24.199.128.3', '31.173.152.1', '186.115.160.2'] adds = locations(ips) self.assertEqual(len(adds), 4) 

    geoip.py

    #!/usr/bin/python3 import geoip2.database, logging geoipfile = "./GeoLite2-City.mmdb" read = geoip2.database.Reader(geoipfile) def locations(ips: list[str]) -> list: s = location() next(s) return [s.send(x) for x in ips] def location() -> list: ip = "1.0.0.1" while True: try: respOnse= read.city(ip) a = [ip, response.location.longitude, response.location.latitude] except Exception as e: logging.exception(e) a = [] ip = yield a 
    Supplement 1    Jul 10, 2021
    变量名问题.
    9 replies    2021-07-10 17:01:46 +08:00
    simple2025
        1
    simple2025  
       Jul 4, 2021
    ```
    from .geoip import location

    ```
    这个改成绝对路劲看一下?
    JasperHale
        2
    JasperHale  
    OP
       Jul 4, 2021
    @chenqh 改成绝对路径也不行. 无论是绝对路径还是相对路径,都找不到测试文件...
    只要 `#read = geoip2.database.Reader(geoipfile)` 注释掉 read 马上就能找到..的
    JasperHale
        3
    JasperHale  
    OP
       Jul 4, 2021
    @chenqh 就是我把其他都去掉,新建一个 .py,文件 只有 `read = geoip2.database.Reader(geoipfile)`,
    测试文件只要 from import 或者 import 就找不到测试文件....
    simple2025
        4
    simple2025  
       Jul 4, 2021
    @JasperHale

    没有搞懂
    learningman
        5
    learningman  
       Jul 5, 2021 via Android
    换个变量名?怕不是撞了
    no1xsyzy
        6
    no1xsyzy  
       Jul 5, 2021
    报了没有顶级包不能相对路径
    报了 adds = locations(ips) 中 locations 不存在

    - from .geoip import location
    + from geoip import locations

    除此以外,我这边正常
    JasperHale
        7
    JasperHale  
    OP
       Jul 5, 2021
    @no1xsyzy
    @learningman 这两天会详细测试一下,排查变量名,版本,机器等问题.
    JasperHale
        8
    JasperHale  
    OP
       Jul 5, 2021
    @no1xsyzy
    @learningman 谢谢了
    JasperHale
        9
    JasperHale  
    OP
       Jul 10, 2021
    @learningman 确实是变量名的问题,谢谢,已解决
    About     Help     Advertise     Blog     API     FAQ     Solana     5150 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 65ms UTC 08:34 PVG 16:34 LAX 01:34 JFK 04:34
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86