V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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
V2EX  ›  Python

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

  •  
  •   JasperHale ·
    jasper-1024 · 2021-07-04 19:24:20 +08:00 · 1351 次点击
    这是一个创建于 998 天前的主题,其中的信息可能已经有所发展或是发生改变。

    楼主非专业 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
    
    第 1 条附言  ·  2021-07-10 17:02:03 +08:00
    变量名问题.
    9 条回复    2021-07-10 17:01:46 +08:00
    chenqh
        1
    chenqh  
       2021-07-04 19:55:12 +08:00
    ```
    from .geoip import location

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

    没有搞懂
    learningman
        5
    learningman  
       2021-07-05 00:25:55 +08:00 via Android
    换个变量名?怕不是撞了
    no1xsyzy
        6
    no1xsyzy  
       2021-07-05 10:11:09 +08:00
    报了没有顶级包不能相对路径
    报了 adds = locations(ips) 中 locations 不存在

    - from .geoip import location
    + from geoip import locations

    除此以外,我这边正常
    JasperHale
        7
    JasperHale  
    OP
       2021-07-05 12:33:27 +08:00
    @no1xsyzy
    @learningman 这两天会详细测试一下,排查变量名,版本,机器等问题.
    JasperHale
        8
    JasperHale  
    OP
       2021-07-05 12:33:42 +08:00
    @no1xsyzy
    @learningman 谢谢了
    JasperHale
        9
    JasperHale  
    OP
       2021-07-10 17:01:46 +08:00
    @learningman 确实是变量名的问题,谢谢,已解决
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5836 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 06:19 · PVG 14:19 · LAX 23:19 · JFK 02:19
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.