繁体   English   中英

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

[英]Python unittests used in a project structure with multiple directories

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

.
├── 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

但是 output 是:

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

Ran 0 tests in 0.000s

OK

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

谢谢你帮助我!

缺少一些文件__init__.py

我认为问题是您的子文件夹中缺少文件__init__.py 尝试将此(空)文件添加到所有子文件夹中,如下所示:

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

如果您观看我的树文件夹,我会创建一个文件夹test_lcm作为树的根目录。 您必须执行cd命令才能将自己置于该文件夹中。
所以执行类似下面的cd命令(在我的系统中test_lcm是放在我的home文件夹下的):

# go to test_lcm folder
cd ~/test_lcm

之后执行:

# execute test
python3 -m unittest discover

output的最后一部分是:

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

FAILED (failures=1, errors=2)

这表明执行了 6 个测试,出现 2 个错误( test_lcm_better_exceptiontest_lcm_exception失败)。

一个有用的链接

是了解如何定义 Python package 的有用链接。

我特别想强调以下句子:需要__init__.py文件才能使 Python 将包含该文件的目录视为包。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM