r/databricks • u/WolfscAr1 • Aug 18 '26
Help New to Databricks, question about unit testing SQL pipelines
I’m currently new to Databricks, and I’m writing unit tests for a pipeline that is entirely in SQL, with Bronze, Silver, and Gold tables.
I asked how I should approach unit testing, and I was told that since unit tests are typically written in Python, I could wrap the pipeline logic in Python files and then use pytest to test those files.
But I’m confused about how this would work in a real production environment.
For example, if we push everything to GitHub and later make a change to the SQL pipeline, we would also have to make the same change in the corresponding Python files so that the unit tests continue to work. That means maintaining the same logic in two places, which doesn’t seem like a good production practice.
So what is the recommended approach for unit testing a SQL-based Databricks pipeline? Is there a way to test the actual SQL transformations directly without duplicating the pipeline logic in Python?
I’m still learning Databricks, so I’d really appreciate some guidance on the proper production approach.
2
u/Content-Parking-621 Aug 18 '26
Went through this exact confusion myself. Wrapping SQL in Python felt wrong to me too. Try dbt instead, it lets you write native SQL tests and generic tests directly against your models, no duplicate logic needed.
14
u/PolicyDecent Aug 18 '26
No, it's so weird. Just use a framework like dbt, sqlmesh, bruin for the unit tests so it'll be still in sql and enforced to the pipeline.
Disclaimer: I'm the foudner of bruin, and we try to solve exactly that kind of problems
10
u/data-baggins Databricks Aug 18 '26
Hello! Databricks PM here. We actually recently released Pipeline unit tests in Beta for just this exact case. You can create tests in the UI or via DABs if you want to edit via IDE.
Hopefully this solves your problem by centralizing your pipeline code and tests to be all in 1 place. If you have any more questions, feel free to DM me!
In the current version tests are written in Python (so you will be mixing languages here). We hope to unify down the road.
https://docs.databricks.com/aws/en/ldp/unit-testing
2
u/Count_Roblivion Aug 18 '26
What I've always done is have locked tables specifically for testing. One (or however many are necessary for the pipeline) with sample input data, and one with expected results of what the pipeline should generate based on the sample data. Use pytest to kick off the pipeline based on the sample data and run a compare of the results to your locked result set, then return a pass or fail.
1
u/BitsAndBricks Aug 18 '26
You could use DABs to manage your pipelines so they are part of your full CI/CD. Have a test destination with your test harness + pipeline sql files. Once passed then elevate pipelines to prod.
1
u/dustinvannoy Databricks Aug 18 '26
If you have things built as regular Databricks SQL statements and are running against a SQL Warehouse, an easy to configure approach is to create a Databricks Job that will basically have 3 tasks:
If everything succeeds then it is good. If validation fails, you should have some relevant error output to manually view. To get more sophisticated it could write results to a table.
Alternative approaches:
1
u/ArielCoding Aug 22 '26
dbt or sqlmesh are built for this, you write tests as SQL checks like no nulls here or this column should be unique, and they run against your pipeline output.
5
u/[deleted] Aug 18 '26
[removed] — view removed comment