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

2021-07-04 19:24:20 +08:00
 JasperHale

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

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
1384 次点击
所在节点    Python
9 条回复
chenqh
2021-07-04 19:55:12 +08:00
```
from .geoip import location

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

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

- from .geoip import location
+ from geoip import locations

除此以外,我这边正常
JasperHale
2021-07-05 12:33:27 +08:00
@no1xsyzy
@learningman 这两天会详细测试一下,排查变量名,版本,机器等问题.
JasperHale
2021-07-05 12:33:42 +08:00
@no1xsyzy
@learningman 谢谢了
JasperHale
2021-07-10 17:01:46 +08:00
@learningman 确实是变量名的问题,谢谢,已解决

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/787473

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX