4

I have multiple test files in the test folder. 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. 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. 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. Do let me know if any more clarification required on the question.

The conftest.py looks something like this:

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:

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"
3
  • 1
    if __name__ == '__main__': pytest.main(args=['test'])? Commented Dec 10, 2018 at 10:11
  • the 'test' you are referring to are the different test_*.py files? Commented Dec 10, 2018 at 10:46
  • @hoefling the solution works when we put test.py outside the test folder and call it. Is there any possibility that I can place the test.py inside the test folder and try running the testfiles through test.py Commented Dec 10, 2018 at 11:11

3 Answers 3

1

I'd recommend just using a bash script and use that to call command line python calls. For example, in you bash script you can just write:

pytest test/

to run all tests within the test directory. 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. Look at pytest Documentation for reference.

Sign up to request clarification or add additional context in comments.

3 Comments

We don;t give external link as a solution rather explain the user your answer and may be can give link for more reference.
My question was I want to have a master .py file where I would call all the other test_*.py files. A bash script would do my work but I dont want to do this through bash script
1

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.

In this example, I want to run all tests that start with the name 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 tests/testpi* | xargs pytest -v

Comments

0

You create folder tests/ and input all file test into this. Run terminal:

pytest -v $(ls tests/**/*.py)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.