[Python] pytest throwing module no found in Databricks' notebook

Discussão em 'Python' iniciado por Stack, Janeiro 11, 2025.

  1. Stack

    Stack Membro Participativo

    I'm recreating below test scenario but constantly getting below error:

    https://learn.microsoft.com/en-us/azure/databricks/notebooks/testing

    ImportError while importing test module '/Workspace/python_tests/dummy_test.py'.
    Hint: make sure your test modules/packages have valid Python names.
    Traceback:
    /usr/lib/python3.12/importlib/__init__.py:90: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
    E ModuleNotFoundError: No module named 'dummy_test'
    =========================== short test summary info ============================
    ERROR dummy_test.py
    !!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
    =============================== 1 error in 0.45s ===============================


    This is the structure of my /Workspace/python_tests directory and each file:

    1. dummy.py

    import pyspark
    from pyspark.sql import SparkSession
    from pyspark.sql.functions import col

    # Because this file is not a Databricks notebook, you
    # must create a Spark session. Databricks notebooks
    # create a Spark session for you by default.
    spark = SparkSession.builder.appName('integrity-tests').getOrCreate()

    def dummy():
    return "I am a dummy"

    1. dummy_test.py

    # import pytest
    import pyspark
    from dummy import *


    def test_dummy():
    assert dummy() == "I am a dummy"

    1. pyTestRunner (notebook)

    %pip install pytest

    import pytest
    import sys

    # Skip writing pyc files on a readonly filesystem.
    sys.dont_write_bytecode = True

    # Run pytest.
    retcode = pytest.main([".", "-v", "-p", "no:cacheprovider"])

    # Fail the cell execution if there are any test failures.
    assert retcode == 0, "The pytest invocation failed. See the log for details."

    Continue reading...

Compartilhe esta Página