我想從本站運行pytest使用wordcount測試 - Unit testing Apache Spark with py.test。問題是我無法啓動火花上下文。代碼我用來運行星火語境:用pytest測試Spark - 無法在本地模式下運行Spark
@pytest.fixture(scope="session")
def spark_context(request):
""" fixture for creating a spark context
Args:
request: pytest.FixtureRequest object
"""
conf = (SparkConf().setMaster("local[2]").setAppName("pytest-pyspark-local-testing"))
sc = SparkContext(conf=conf)
request.addfinalizer(lambda: sc.stop())
quiet_py4j()
return sc
我使用命令執行此代碼:
#first way
pytest spark_context_fixture.py
#second way
python spark_context_fixture.py
輸出:
platform linux2 -- Python 2.7.5, pytest-3.0.4, py-1.4.31, pluggy-0.4.0
rootdir: /home/mgr/test, inifile:
collected 0 items
然後我想用pytest運行wordcount的測試。
pytestmark = pytest.mark.usefixtures("spark_context")
def test_do_word_counts(spark_context):
""" test word couting
Args:
spark_context: test fixture SparkContext
"""
test_input = [
' hello spark ',
' hello again spark spark'
]
input_rdd = spark_context.parallelize(test_input, 1)
results = wordcount.do_word_counts(input_rdd)
expected_results = {'hello':2, 'spark':3, 'again':1}
assert results == expected_results
但輸出是:
________ ERROR at setup of test_do_word_counts _________
file /home/mgrabowski/test/wordcount_test.py, line 5
def test_do_word_counts(spark_context):
E fixture 'spark_context' not found
> available fixtures: cache, capfd, capsys, doctest_namespace, monkeypatch, pytestconfig, record_xml_property, recwarn, tmpdir, tmpdir_factory
> use 'pytest --fixtures [testpath]' for help on them.
有誰知道這是什麼問題的原因是什麼?
你在你的機器上安裝了spark嗎? – Yaron
是的,我安裝了Spark 1.6。我能夠在命令行中運行pyspark,因此看起來沒問題。 –