Coverage.py在子目录中没有init.py文件的情况下无法发现测试 [英] Coverage.py does not discover tests without init.py file in sub directories

查看:89
本文介绍了Coverage.py在子目录中没有init.py文件的情况下无法发现测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行python的coverage时,我总是需要在tests子目录中有一个空的__init__.py文件来获取覆盖率以运行测试.这是python2软件包的要求,但不是python3的要求.为了进行复制,我做了以下工作(前提条件是python3,pip3和brew):

When I run coverage for python, I always need an empty __init__.py file in the tests sub-directory to get coverage to run the tests. This is a requirement for python2 packages, but not for python3. To reproduce, I did the following (pre-requisites are python3, pip3 and brew):

  1. 运行以下终端命令:

  1. Run the following terminal command:

pip3 install coverage

  • 创建以下目录结构:

  • Create the following directory structure:

    example\
        example.py
    tests\
        test_example.py
    

  • example.py:

    example.py:

    #!/usr/bin/env python3
    class Example:
        value = 3
    
        def update(self):
            self.value = 4
    

    test_example.py:

    test_example.py:

    #!/usr/bin/env python3
    
    import unittest
    from example.example import Example
    
    class TestExample(unittest.TestCase):
        def test_example(self):
            example_object = Example()
            self.assertEqual(3, example_object.value)
            example_object.update()
            self.assertEqual(4, example_object.value)
    

    1. 运行以下终端命令:

    1. Run the following terminal command:

    coverage run --branch -m unittest discover -s . && coverage report
    

    我应该得到:Ran 1 test in x.yz seconds,但是我总是得到Ran 0 tests in x.yz seconds,并且要解决此问题,我必须将__init__.py文件添加到两个目录中.如何在不需要初始化文件的情况下进行覆盖?

    I should get: Ran 1 test in x.yz seconds, but I always get Ran 0 tests in x.yz seconds, and to fix this, I have to add __init__.py files to both directories. How can I run coverage without needing the init files?

    如果您有其他需要解决的问题,请告诉我.我将不胜感激!

    Please let me know if you need anything else from me regarding this question. I would appreciate any help!

    推荐答案

    尽管Python 3不需要__init__.py文件,但忽略它们会创建名称空间包,如果不需要它们,这不是一个好主意.例如,它们是最后读取的,因此,即使以后在PYTHONPATH中的目录也可以隐藏文件.

    Although Python 3 doesn't need __init__.py files, omitting them creates namespace packages, which are not a good idea if you don't need them. For example, they are read last, so even directories later in the PYTHONPATH can shadow your files.

    您仍然应该创建__init__.py文件.

    这篇关于Coverage.py在子目录中没有init.py文件的情况下无法发现测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆