在具有多个目录的项目结构中使用 Python 单元测试

2024-03-03

我需要使用unittestpython 库来执行有关 3 个函数的测试src/arithmetics.py文件。这是我的项目结构。

.
├── src
│   └── arithmetics.py
└── test
    └── lcm
        ├── __init__.py
        ├── test_lcm_exception.py
        └── test_lcm.py

src/arithmetics.py

def lcm(p, q):
    p, q = abs(p), abs(q)
    m = p * q
    while True:
        p %= q
        if not p:
            return m // q
        q %= p
        if not q:
            return m // p

def lcm_better(p, q):
    p, q = abs(p), abs(q)
    m = p * q
    h = p % q
    while h != 0:
        p = q
        q = h
        h = p % q
    h = m / q
    return h

def lcm_faulty(p, q):
    r, m = 0, 0
    r = p * q
    while (r > p) and (r > q):
        if (r % p == 0) and (r % q == 0):
            m = r
        r = r - 1
    return m

test/lcm/test_lcm.py

import unittest
from src.arithmetics import *

class LcmTest(unittest.TestCase):
    def test_lcm(self):
        for X in range(1, 100):
            self.assertTrue(0 == lcm(0, X))
            self.assertTrue(X == lcm(X, X))

        self.assertTrue(840 == lcm(60, 168))
    
    def test_lcm_better(self):
        for X in range(1, 100):
            self.assertTrue(0 == lcm_better(0, X))
            self.assertTrue(X == lcm_better(X, X))
    
        self.assertTrue(840 == lcm_better(60, 168))
    
    def test_lcm_faulty(self):
        self.assertTrue(0 == lcm_faulty(0, 0))
    
        for X in range(1, 100):
            self.assertTrue(0 == lcm_faulty(X, 0))
            self.assertTrue(0 == lcm_faulty(0, X))
    
        self.assertTrue(840 == lcm_faulty(60, 168))

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

test/lcm/test_lcm_exception.py

import unittest
from src.arithmetics import *

class LcmExceptionTest(unittest.TestCase):
    def test_lcm_exception(self):
        for X in range(0, 100):
            self.assertTrue(0 == lcm(0, 0))         # ZeroDivisionError
            self.assertTrue(0 == lcm(X, 0))         # ZeroDivisionError

    def test_lcm_better_exception(self):
        for X in range(0, 100):
            self.assertTrue(0 == lcm_better(0, 0))  # ZeroDivisionError
            self.assertTrue(0 == lcm_better(X, 0))  # ZeroDivisionError
    
    def test_lcm_faulty_exception(self):
        for X in range(1, 100):
            self.assertTrue(X == lcm_faulty(X, X))  # ppcm(1, 1) != 1

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

test/lcm/__init__.py是一个空文件

为了执行我的测试,我尝试了这个命令:python3 -m unittest discover

但输出是:

----------------------------------------------------------------------

Ran 0 tests in 0.000s

OK

我不明白如何运行我的测试......

谢谢你帮助我!


A file __init__.py不见了

我认为问题是缺少文件__init__.py在文件夹中test。尝试在该文件夹中添加这个(空)文件,如下所示:

test_lcm
├── src
│   └── arithmetics.py
└── test
    └── __init__py    <---------------- add this file
    └── lcm
        ├── __init__.py
        ├── test_lcm_exception.py
        └── test_lcm.py

如果您观看我的树文件夹,我已经创建了一个文件夹test_lcm作为树的根。你必须执行cd命令将自己置于该文件夹中。
所以执行一个cd命令类似于以下内容(在我的系统中test_lcm放置在我的主文件夹中):

# go to test_lcm folder
cd ~/test_lcm

之后,执行:

# execute test
python3 -m unittest discover

输出的最后一部分是:

----------------------------------------------------------------------
Ran 6 tests in 0.002s

FAILED (failures=1, errors=2)

输出显示执行了 6 个测试,有 2 个错误(test_lcm_better_exception and test_lcm_exception fail).

有用的链接

  • This https://docs.python.org/3/tutorial/modules.html#packages是了解如何定义 Python 包的有用链接。

我特别想强调该链接中存在的以下句子:

The __init__.py需要文件才能使 Python 将包含该文件的目录视为包。

  • Instead 这是一个帖子 https://stackoverflow.com/questions/1896918/running-unittest-with-typical-test-directory-structure其中谈到了类似的话题。

  • 此外这个链接 https://dev.to/methane/don-t-omit-init-py-3hga报道这句话:

例如,标准库中的unittest模块不会在没有以下内容的情况下搜索目录__init__.py.

这解释了为什么该文件__init__.py是必要的。可能在未来的unittest模块也会在没有的目录中搜索测试__init__.py file.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在具有多个目录的项目结构中使用 Python 单元测试 的相关文章

随机推荐