【问题标题】:Importing modules properly for unit testing in Python using pyunit使用 pyunit 正确导入模块以在 Python 中进行单元测试
【发布时间】:2017-06-30 20:11:01
【问题描述】:

关于单元测试的问题

目标:目标是使用testCalc.py中的pyUnit对calculator.py中的simpleCalc对象进行单元测试。
问题:我无法成功导入当 testCalc 从项目中的单独目录运行时,simpleCalc 对象从calculator.py 到 testCalc.py。
背景:当 testCalc.py 中的单元测试包含在同一个目录中时,它运行得非常好目录为calculator.py,但是当我将它移动到一个单独的文件夹并尝试导入在calculator.py 中定义的simpleCalc 对象时,我得到一个错误。我正在尝试学习如何在一个简单的项目中使用 pyUnit 单元测试框架,但我显然缺少一些关于如何在分层目录结构中导入模块以进行单元测试的基本知识。下面描述的基本calculator_test 项目是我为练习而创建的一个简单项目。您可以在这篇文章的末尾看到我已经浏览过的所有文章。
终极问题:如何将 simpleCalc 对象导入 testCalc.py 并使用下面描述的目录层次结构?

Github:https://github.com/jaybird4522/calculator_test/tree/unit_test

这是我的目录结构:

calculator_test/
    calculatorAlgo/
        __init__.py
        calculator.py
    test_calculatorAlgo/
        __init__.py
        testCalc.py
        testlib/
            __init__.py
            testcase.py

这是calculator.py 文件,它描述了我要单元测试的simpleCalc 对象:

# calculator.py

class simpleCalc(object):

    def __init__(self):
        self.input1 = 0
        self.input2 = 0

    def set(self, in1, in2):
        self.input1 = in1
        self.input2 = in2

    def subtract(self, in1, in2):
        self.set(in1, in2)
        result = self.input1 - self.input2
        return result

这是 testCalc.py 文件,其中包含单元测试:

# testCalc.py

import unittest
from calculatorAlgo.calculator import simpleCalc

class testCalc(unittest.TestCase):

    # set up the tests by instantiating a simpleCalc object as calc
    def setUp(self):
        self.calc = simpleCalc()

    def runTest(self):
        self.assertEqual(self.calc.subtract(7,3),4)

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

我一直在用简单的命令运行单元测试文件:

testCalc.py

到目前为止我所做的尝试

第一次尝试
我尝试根据它在目录结构中的位置简单地导入 simpleCalc 对象:

# testCalc.py

import unittest
from .calculatorAlgo.calculator import simpleCalc

class testCalc(unittest....

得到了这个错误:
ValueError: Attempted relative import in non-package

第二次尝试
我尝试在没有相关引用的情况下导入它:

# testCalc.py

import unittest
import simpleCalc

class testCalc(unittest....

得到了这个错误:
ImportError: No module named simpleCalc

第三次尝试
基于这篇 http://blog.aaronboman.com/programming/testing/2016/02/11/how-to-write-tests-in-python-project-structure/ 的帖子,我尝试创建一个名为 testcase.py 的单独基类,它可以执行相对导入。

# testcase.py

from unittest import TestCase
from ...calculator import simpleCalc

class BaseTestCase(TestCase):
    pass

并在 testCalc.py 中更改了我的导入

# testCalc.py

import unittest
from testlib.testcase import BaseTestCase

class testCalc(unittest....

得到了这个错误:
ValueError: Attempted relative import beyond toplevel package

其他资源
以下是我处理过但无济于事的一些帖子:
Import a module from a relative path
python packaging for relative imports
How to fix "Attempted relative import in non-package" even with __init__.py
Python importing works from one folder but not another
Relative imports for the billionth time

最终,我觉得我只是缺少一些基本的东西,即使经过大量研究。这感觉像是一个常见的设置,我希望有人能告诉我我做错了什么,并且它可能会帮助其他人在未来避免这个问题。

【问题讨论】:

    标签: python unit-testing python-import python-unittest


    【解决方案1】:

    在 testCalc.py 文件中,添加以下内容。

    import sys
    import os
    
    sys.path.append(os.path.abspath('../calculatorAlgo'))
    from calculator import simpleCalc
    

    【讨论】:

    • 我添加了一个缺少的正斜杠。
    • 弄乱路径对我来说似乎很老套。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-06
    • 2022-10-18
    • 2019-06-21
    • 2013-03-25
    • 2012-08-08
    相关资源
    最近更新 更多