无法将自定义模块导入测试或主服务器

时间:2019-03-25 22:14:17

标签: python unit-testing import tox

我正在设置单元测试和Flask Server,以测试我公司的某些集成,即病毒总API密钥验证,并且在导入一些我编写的模块时遇到了麻烦。

# tests/vt.py

from src import VirusTotal
import os
import unittest

VirusTotal = VirusTotal()

# docker
# KEY = os.environ['VTAPIKEY']
# POISONKEY = os.environ['VTPOISONEDKEY']

# localhost disable when in docker
KEY = ${KEY}
POISONKEY = ${POISONKEY}


class RESTAPI(unittest.TestCase):
    """Test virus total endpoints."""

    def checkAPI(self):
        """Look into vtotal API."""
        response = VirusTotal.check_virus({"apikey": KEY})
        self.assertIs(response["result"], True)

    def poisonCheckAPI(self):
        """Look into sending bad data to vtotal API."""
        response = VirusTotal.check_virus(
            {"apikey": POISONKEY})
        self.assertIs(response["result"], False)

    def tearDown(self):
        """Run at end of test cases."""
        print("\n------- test is over -------\n")


if __name__ == "__main__":
    unittest.main()


当我运行Tox(测试自动化工具)或常规测试时 它不起作用,我从控制台收到错误信息 在下面给出。

# Error Message
Traceback (most recent call last):
  File "tests/vt.py", line 1, in <module>
    from src import VirusTotal
ModuleNotFoundError: No module named 'src'

src和tests目录都存储在项目中 根目录,并且其中都有一个 init .py文件 这是src中的那个

# src/__init__.py
from src.vt import VirusTotal

0 个答案:

没有答案