10

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

 4 years ago
source link: https://www.v2ex.com/t/787473
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

V2EX  ›  Python

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

  JasperHale · jasper-1024 · 21 分钟前 · 22 次点击

楼主非专业 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

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK