pytest框架为什么能够识别test命名的文件

使用pytest时候,我们知道要遵循一定命名规则,以 test_ 开头的函数名或者以 test_*.py 命名的文件会被默认收集,但这个原理你知道吗

见代码07800db385284881a0113115d5bccd23.jpeg

 

pytest 自动收集测试函数的功能:

```python

import os

import importlib

import inspect

 

def collect_tests(start_dir):

    test_functions = []

 

    for root, dirs, files in os.walk(start_dir):

        for file in files:

            if file.startswith("test_") and file.endswith(".py"):

                module_name = file[:-3] # Remove the .py extension

                module_path = os.path.join(root, file)

                

                # Dynamically import the module

                module = importlib.import_module(module_name)

 

                # Inspect all attributes in the module

                for name, obj in inspect.getmembers(module):

                    if inspect.isfunction(obj) and name.startswith("test_"):

                        test_functions.append(obj)

 

    return test_functions

 

# Example usage

test_functions = collect_tests("/path/to/tests_directory")

 

for test_func in test_functions:

    print(f"Found test function: {test_func.__name__}")

    # You can now execute the test functions as needed

```

 

在这个示例中,`collect_tests` 函数接受一个起始目录作为参数,递归搜索该目录及子目录下的以 `test_*.py` 命名的文件,并导入这些文件中的模块。然后,通过检查模块中的所有成员,筛选出以 `test_` 开头的函数,并将其存储在 `test_functions` 列表中。

 

最后,可以遍历 `test_functions` 列表,获取到所有收集到的测试函数,并执行相应的测试。

 

看代码os.walk和file.startswith("test_") and file.endswith(".py"):

这个示例代码展示了如何使用 importlib 模块和反射机制来实现测试函数的自动收集,并可以根据实际需求进行扩展和修改

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
pytest框架不能import本地文件的原因是因为pytest默认只在指定的测试目录下查找测试文件,而不在其他目录中查找。这是为了确保测试的独立性和可重复性。如果你想在pytest中import本地文件,你可以将本地文件放在指定的测试目录下,或者使用相对路径来引用本地文件。另外,你也可以在pytest的配置文件pytest.ini中配置导入本地文件的路径。具体的配置方法可以参考官方文档或者使用第三方插件来实现。\[2\]\[3\] #### 引用[.reference_title] - *1* [Python语言+pytest框架+allure报告+log日志+yaml文件+mysql断言实现接口自动化框架](https://blog.csdn.net/ZangKang1/article/details/127499183)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [python的pytest框架](https://blog.csdn.net/legend818/article/details/105450904)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Pthon pytest框架标签定制化执行](https://blog.csdn.net/qq_40207262/article/details/129633178)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

专注软件测试报告的摄影

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值