重新组织文件结构后,unittest没有运行我的测试

时间:2017-10-04 20:46:31

标签: python python-3.x

这是我的结构:

directory/
      __init__.py (blank)
      myClass.py
      test/
             UnitTest.py
             IntegrationTest.py
             __init__.py (blank)

让我们使用UnitTest作为示例(它们都导致0次测试)。随意批评我的导入,我正在摆弄它永远正确导入类,所以我可以执行UnitTest脚本。同样是的,我在搜索中从其他堆栈交换问题中复制了大量代码。

在我搬到这个结构之前,我把所有东西放在一个目录中,所以我知道测试文件有效(在导入之外)

import sys
from pathlib import Path

if __name__ == '__main__' and __package__ is None:
    file = Path(__file__).resolve()
    parent, top = file.parent, file.parents[2]

    sys.path.append(str(top))
    try:
        sys.path.remove(str(parent))
    except ValueError: # Already removed
        pass

    import directory.test
    __package__ = 'directory.test'

from myClass import myClass
import unittest
from unittest import mock

print("Running Unit Tests...")
mc = myClass(params)

def mockingResponse(*args, **kwargs):
    class MockResponse:
        def __init__(self, json_data, status_code):
            self.json_data = json_data
            self.status_code = status_code

        def json(self):
            return self.json_data

    #if/elses that return value

@mock.patch('myClass.requests.get', side_effect = mockingResponse)

class unitTest(unittest.TestCase):
    print("this should run before test call")
    def testsomefunc():
        response = mc.somefunc()
        differenceSet = set(response) ^ set(mockrespose from if/elses)
        assert len(differenceSet) == 0
    def testotherfunc():
        #7 tests in total, same layout, different mockresponse

print("is this actually running")
unittest.main()

然后我在终端得到这个:

privacy:~/git/directory$ python3 -m test.UnitTest.py 
Running Unit Tests...
this should run before test call
is this actually running

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

在网上查询,我知道这不是我的名字从测试开始的问题,而不是因为我的错误缩进。在我的搜索中找不到其他内容。

我唯一的另一个想法是,它可能是因为我将它作为模块运行,但如果我不这样做,我导入myClass会遇到更多问题。在我尝试导入的众多解决方案中,这是迄今为止唯一有效的解决方案。

0 个答案:

没有答案
相关问题