cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Certifications
Join dynamic discussions on Databricks certifications within the Community. Exchange insights, tips, and experiences to help prepare for certification exams and validate your expertise in data engineering, analytics, and machine learning.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

DevOps Fundamentals: 08 - Creating and Executing Unit Tests: Instructions doesn't match code

Daniel_B
Contributor

Instructions: 

To execute pytest within Databricks complete the following:

  • sys.dont_write_bytecode = True - By default, Python generates .pyc bytecode files in a pycache directory. Setting it to True prevents this, which is useful in read-only environments.

Code in cell directly underneath:

  • sys.dont_write_bytecode = False

Thanks. I hereby claim my $1000000 bug bounty. You're welcome.

 

1 REPLY 1

SumeshKashyap
Valued Contributor II

Good catch - the instruction text is right and the code cell is the typo. It should be sys.dont_write_bytecode = True. With False (Python's default) the tests still run, since Python silently ignores a failed .pyc write, but on a writable Git folder you end up with __pycache__ folders next to your test files.

Use the same pattern as the Databricks unit-testing docs:

 

import pytest, sys

sys.dont_write_bytecode = True  # skip .pyc files on a read-only filesystem

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

assert retcode == 0, "The pytest invocation failed. See the log for details."

-p no:cacheprovider is the companion setting - it stops pytest writing its .pytest_cache folder for the same reason.

If you already ran the lab with False and see __pycache__ folders in your repo, delete them (or add __pycache__/ to .gitignore) so they don't get committed.

 

To get the notebook fixed for other learners, report it to the Academy team: https://help.databricks.com/s/contact-us?ReqType=training

Docs: https://docs.databricks.com/aws/en/notebooks/testing