繁体   English   中英

通过 python Pytest 从一个 test.py 文件运行多个测试文件

[英]Running multiple test files from one test.py file through python Pytest

我在 test 文件夹中有多个测试文件。 结构类似于这样:

/test
----test_abc.py
----test_bcd.py
----test_cde.py
----conftest.py

conftest.py 包含运行单元测试所需的所有 spark 上下文初始化。 我的问题是我想要一个test.py文件,它在内部触发所有test_abc.pytest_bcd.pytest_cde.py 当我们处理 python 的utit_test模块时变得非常容易,但我不确定如何通过pytest模块获得它。 请让我知道是否需要对该问题进行更多说明。

conftest.py 看起来像这样:

import pytest
from pyspark import SQLContext
from pyspark import SparkConf
from pyspark import SparkContext
from pyspark.streaming import StreamingContext

@pytest.fixture(scope="session")
def spark_context(request):
    conf = (SparkConf().setMaster("local[2]").setAppName("pytest-pyspark-local-testing"))
    request.addfinalizer(lambda: sc.stop())
    sc = SparkContext(conf=conf).getOrCreate()
    return sc

其中一个 test_abc.py 看起来像这样:

import pytest
import os
from pyspark.sql import SQLContext
pytestmark = pytest.mark.usefixtures("spark_context")
def test_get_pc_browser_sql(spark_context):
    "assert something"

我建议只使用 bash 脚本并使用它来调用命令行 python 调用。 例如,在你的 bash 脚本中,你可以只写:

pytest test/

运行test目录中的所有测试。 您还可以添加所有参数命令和在命令行上使用的任何命令。 然后,只需为要测试的特定文件或目录集执行 bash 脚本。 查看pytest 文档以供参考。

我有类似的需求,我只想运行一些测试文件而不是整个文件夹。 为了让 pytest 使用单个命令运行多个文件,如果您重命名要作为一个组运行的文件,bash 提供了几种不同的方法来运行这样一个组。

在此示例中,我想运行所有以testpi名称testpi测试:

  1. 收集所有匹配的文件并放入一个字符串中:
$ pytest -v $(ls tests/testpi*)
  1. ls的输出通过管道传输到xargs以执行相同的操作:
ls tests/testpi* | xargs pytest -v

暂无
暂无

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

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