[英]Running multiple test files from one test.py file through python Pytest
I have multiple test files in the test folder.我在 test 文件夹中有多个测试文件。 The structure is similar to something like this:结构类似于这样:
/test
----test_abc.py
----test_bcd.py
----test_cde.py
----conftest.py
The conftest.py contains all the spark context initialization which is necessary for running the unit test. conftest.py 包含运行单元测试所需的所有 spark 上下文初始化。 My problem is I would like to have a test.py
file which internally triggers all the test_abc.py
, test_bcd.py
and test_cde.py
.我的问题是我想要一个test.py
文件,它在内部触发所有test_abc.py
、 test_bcd.py
和test_cde.py
。 It becomes very easy when we deal with utit_test module of python but I am not sure how to obtain this through pytest module.当我们处理 python 的utit_test模块时变得非常容易,但我不确定如何通过pytest模块获得它。 Do let me know if any more clarification required on the question.请让我知道是否需要对该问题进行更多说明。
The conftest.py looks something like this: 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
And one of the test_abc.py looks something like this:其中一个 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"
I'd recommend just using a bash script and use that to call command line python calls.我建议只使用 bash 脚本并使用它来调用命令行 python 调用。 For example, in you bash script you can just write:例如,在你的 bash 脚本中,你可以只写:
pytest test/
to run all tests within the test
directory.运行test
目录中的所有测试。 You can also add all argument commands and any commands that use on the command line.您还可以添加所有参数命令和在命令行上使用的任何命令。 Then, just execute the bash script for the specific set of files or directories to test.然后,只需为要测试的特定文件或目录集执行 bash 脚本。 Look at pytest Documentation for reference.查看pytest 文档以供参考。
I had a similar need where I want to run only some test files and not an entire folder.我有类似的需求,我只想运行一些测试文件而不是整个文件夹。 In order to have pytest run several files with a single command, if you rename the files you want to run as a group, bash provides a couple of different ways to run such a group.为了让 pytest 使用单个命令运行多个文件,如果您重命名要作为一个组运行的文件,bash 提供了几种不同的方法来运行这样一个组。
In this example, I want to run all tests that start with the name testpi
:在此示例中,我想运行所有以testpi
名称testpi
测试:
$ pytest -v $(ls tests/testpi*)
ls
to xargs
to do the same:将ls
的输出通过管道传输到xargs
以执行相同的操作:ls tests/testpi* | xargs pytest -v
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.