تشغيل الاختبارات باستخدام pytest باستخدام ملحق Databricks ل Visual Studio Code
توضح هذه المقالة كيفية تشغيل الاختبارات باستخدام pytest
ملحق Databricks ل Visual Studio Code. راجع ما هو ملحق Databricks ل Visual Studio Code؟.
يمكنك تشغيل pytest على التعليمات البرمجية المحلية التي لا تحتاج إلى اتصال بمجموعة في مساحة عمل Azure Databricks بعيدة. على سبيل المثال، قد تستخدم pytest
لاختبار الوظائف التي تقبل وتعيد PySpark DataFrames في الذاكرة المحلية. لبدء الاستخدام pytest
وتشغيله محليا، راجع بدء الاستخدام في pytest
الوثائق.
للتشغيل pytest
على التعليمات البرمجية في مساحة عمل Azure Databricks بعيدة، قم بما يلي في مشروع Visual Studio Code الخاص بك:
الخطوة 1: إنشاء الاختبارات
أضف ملف Python مع التعليمات البرمجية التالية، والتي تحتوي على اختباراتك لتشغيلها. يفترض هذا المثال أن هذا الملف مسمى spark_test.py
وهو في جذر مشروع Visual Studio Code. يحتوي هذا الملف على مثبت pytest
، ما يجعل نظام المجموعة SparkSession
(نقطة الإدخال إلى وظيفة Spark على نظام المجموعة) متاحا للاختبارات. يحتوي هذا الملف على اختبار واحد يتحقق مما إذا كانت الخلية المحددة في الجدول تحتوي على القيمة المحددة. يمكنك إضافة الاختبارات الخاصة بك إلى هذا الملف حسب الحاجة.
from pyspark.sql import SparkSession
import pytest
@pytest.fixture
def spark() -> SparkSession:
# Create a SparkSession (the entry point to Spark functionality) on
# the cluster in the remote Databricks workspace. Unit tests do not
# have access to this SparkSession by default.
return SparkSession.builder.getOrCreate()
# Now add your unit tests.
# For example, here is a unit test that must be run on the
# cluster in the remote Databricks workspace.
# This example determines whether the specified cell in the
# specified table contains the specified value. For example,
# the third column in the first row should contain the word "Ideal":
#
# +----+-------+-------+-------+---------+-------+-------+-------+------+-------+------+
# |_c0 | carat | cut | color | clarity | depth | table | price | x | y | z |
# +----+-------+-------+-------+---------+-------+-------+-------+------+-------+------+
# | 1 | 0.23 | Ideal | E | SI2 | 61.5 | 55 | 326 | 3.95 | 3. 98 | 2.43 |
# +----+-------+-------+-------+---------+-------+-------+-------+------+-------+------+
# ...
#
def test_spark(spark):
spark.sql('USE default')
data = spark.sql('SELECT * FROM diamonds')
assert data.collect()[0][2] == 'Ideal'
الخطوة 2: إنشاء مشغل pytest
أضف ملف Python مع التعليمات pytest
البرمجية التالية، والتي ترشد إلى تشغيل الاختبارات الخاصة بك من الخطوة السابقة. يفترض هذا المثال أن الملف مسمى pytest_databricks.py
وهو في جذر مشروع Visual Studio Code.
import pytest
import os
import sys
# Run all tests in the connected directory in the remote Databricks workspace.
# By default, pytest searches through all files with filenames ending with
# "_test.py" for tests. Within each of these files, pytest runs each function
# with a function name beginning with "test_".
# Get the path to the directory for this file in the workspace.
dir_root = os.path.dirname(os.path.realpath(__file__))
# Switch to the root directory.
os.chdir(dir_root)
# Skip writing .pyc files to the bytecode cache on the cluster.
sys.dont_write_bytecode = True
# Now run pytest from the root directory, using the
# arguments that are supplied by your custom run configuration in
# your Visual Studio Code project. In this case, the custom run
# configuration JSON must contain these unique "program" and
# "args" objects:
#
# ...
# {
# ...
# "program": "${workspaceFolder}/path/to/this/file/in/workspace",
# "args": ["/path/to/_test.py-files"]
# }
# ...
#
retcode = pytest.main(sys.argv[1:])
الخطوة 3: إنشاء تكوين تشغيل مخصص
للحصول على تعليمات pytest
لتشغيل الاختبارات الخاصة بك، يجب إنشاء تكوين تشغيل مخصص. استخدم تكوين التشغيل المستند إلى نظام مجموعة Databricks الحالي لإنشاء تكوين التشغيل المخصص الخاص بك، كما يلي:
في القائمة الرئيسية، انقر فوق تشغيل > إضافة تكوين.
في لوحة الأوامر، حدد Databricks.
يضيف Visual Studio Code ملفا
.vscode/launch.json
إلى مشروعك، إذا لم يكن هذا الملف موجودا بالفعل.قم بتغيير تكوين تشغيل البداية كما يلي، ثم احفظ الملف:
- قم بتغيير اسم تكوين التشغيل هذا من
Run on Databricks
إلى بعض اسم العرض الفريد لهذا التكوين، في هذا المثالUnit Tests (on Databricks)
. - قم بالتغيير
program
من${file}
إلى المسار في المشروع الذي يحتوي على مشغل الاختبار، في هذا المثال${workspaceFolder}/pytest_databricks.py
. - قم بالتغيير
args
من[]
إلى المسار في المشروع الذي يحتوي على الملفات مع اختباراتك، في هذا المثال["."]
.
launch.json
يجب أن يبدو الملف كما يلي:{ // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "type": "databricks", "request": "launch", "name": "Unit Tests (on Databricks)", "program": "${workspaceFolder}/pytest_databricks.py", "args": ["."], "env": {} } ] }
- قم بتغيير اسم تكوين التشغيل هذا من
الخطوة 4: تشغيل الاختبارات
تأكد من تثبيته pytest
بالفعل على نظام المجموعة أولا. على سبيل المثال، مع فتح صفحة إعدادات نظام المجموعة في مساحة عمل Azure Databricks، قم بما يلي:
- في علامة التبويب مكتبات ، إذا كان pytest مرئيا، فهذا
pytest
إذا مثبت بالفعل. إذا لم يكن pytest مرئيا، فانقر فوق تثبيت جديد. - بالنسبة إلى مصدر المكتبة، انقر فوق PyPI.
- بالنسبة للحزمة، أدخل
pytest
. - انقر فوق تثبيت.
- انتظر حتى تتغير الحالة من معلق إلى مثبت.
لتشغيل الاختبارات، قم بما يلي من مشروع Visual Studio Code:
- في القائمة الرئيسية، انقر فوق عرض > تشغيل.
- في القائمة تشغيل وتصحيح ، انقر فوق اختبارات الوحدة (على Databricks)، إذا لم تكن محددة بالفعل.
- انقر فوق أيقونة السهم الأخضر (بدء تصحيح الأخطاء).
pytest
يتم عرض النتائج في وحدة تحكم تتبع الأخطاء (عرض > وحدة تحكم تتبع الأخطاء في القائمة الرئيسية). على سبيل المثال، تظهر هذه النتائج أنه تم العثور على اختبار واحد على الأقل في spark_test.py
الملف، ونقطة (.
) تعني أنه تم العثور على اختبار واحد وتم اجتيازه. (قد يظهر الاختبار الفاشل . F
)
<date>, <time> - Creating execution context on cluster <cluster-id> ...
<date>, <time> - Synchronizing code to /Workspace/path/to/directory ...
<date>, <time> - Running /pytest_databricks.py ...
============================= test session starts ==============================
platform linux -- Python <version>, pytest-<version>, pluggy-<version>
rootdir: /Workspace/path/to/directory
collected 1 item
spark_test.py . [100%]
============================== 1 passed in 3.25s ===============================
<date>, <time> - Done (took 10818ms)