简体   繁体   English

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

[英]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.pytest_bcd.pytest_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测试:

  1. Gathers all matching files and puts then in a string:收集所有匹配的文件并放入一个字符串中:
$ pytest -v $(ls tests/testpi*)
  1. Pipe the output of ls to xargs to do the same:ls的输出通过管道传输到xargs以执行相同的操作:
ls tests/testpi* | xargs pytest -v

暂无
暂无

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

相关问题 ImportError运行Test.py - ImportError Running Test.py 当您只是从其他地方(例如 main.py)导入一段 test.py 时,为什么要运行 python 文件(例如 test.py)的其他代码行? - Why do you run other lines of codes of a python file(say test.py) when you are just importing a piece of test.py from somewhere else (say main.py)? &#39;%%file test.py&#39; 在 python 中是什么意思? - what does '%%file test.py' mean in python? Django test.py model object 实例仅限一次测试 - Django test.py model object instance is limited to one test 如何将单元测试python文件名列表(path \\ test.py)传递给nas.run()方法,以执行所有这些文件中的所有测试? - How can I pass a list of unit test python file names(path\test.py) to nose.run() method to execute all the tests in all these files? 从 python 文件而不是从命令行运行 pytest 测试 - Running a pytest test from a python file and NOT from a command line 无法使用 PyCharm 从 GitHub 存储库访问 test.py 文件 - Cannot access test.py file from GitHub repository using PyCharm ArgParse错误。 但在Test.py文件中工作 - ArgParse ERROR. but working in Test.py file 自动完成test.py像git <tab> - autocomplete for test.py like git <tab> &#39;/ usr / bin / python3:在Raspberry Pi上使用mono执行.exe文件时无法打开文件&#39;test.py&#39;&#39; - '/usr/bin/python3: Can't Open File 'test.py'' when executing .exe file using mono on Raspberry Pi
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM