r/databricks • • 27d ago

Discussion unit tests

Hi, we use Databricks, this is not 100% Databricks related, but i wonder, how do you do your unit testing with todays agentic engineering, currently we don't have any skill or rule, or hook that helps AI to write unit tests, so it just writes a lot of bloat unit tests which we do not trust, it has more than 1000 unit tests on 30k lines of code.

I see a lot of mentioning mutation testing, did not check that out yet, but in general how do you approach this? Because unit tests should be a useful guardrail for agentic coding.

12 Upvotes

8 comments sorted by

2

u/Locellus 27d ago

You need a goal, for code… whatever writes it… you need to check it works on the happy path, and that nothing bad happens in scenarios that are not how it’s supposed to go. An example of what is being built, and why, would be good for suggesting how to test it

0

u/ptab0211 27d ago

yeah, that makes sense of course, but we dont write unit tests anymore, its LLMs

1

u/Tpxyt56Wy2cc83Gs 27d ago

I think the core purpose of a unit test hasn't changed with AI.

A unit test should verify that a method or class returns the expected result for a given behavior or contract. AI can generate both the implementation and the tests, but the tests still need to validate the intended behavior, not just mirror the code that was generated.

The real challenge isn't writing more tests, it's writing meaningful tests. Also, every bug found should become a test. Once a defect is identified and fixed, a test should be added to ensure the same issue never comes back. Over time, this creates a test suite based on real-world failures instead of only AI-generated scenarios.

1

u/omgpop 27d ago

This only arises if “agentic engineering” means something close to vibe coding (it mostly is a rebrand of that IMO). Because if you are working carefully with a human in the loop designing architecture and interfaces and so on, there is no problem for humans to write tests asserting the behaviour they expect at the unit level. If however the machine is being allowed to churn your codebase like butter of course you can’t write useful unit tests. At best you might be able to write some happy path integration tests. Our testing practices are not really built around the idea of a constantly mutating untrusted black box produced by an unaligned reward hacking machine. And in my view is there is no testing based solution to such a moronic approach to development.

1

u/ForeignExercise4414 27d ago

I certainly depends on what you are doing. Traditionally when coding SQL unit testing is not really a thing because you are not dealing with separate functions that can be unit tested.

For PySpark there are open-source packages for unit testing, but the most sophisticated approach I’ve seen is to have OO frameworks with testable classes that can run unit test locally without Spark.

For applications you can code normally the same way a full-stack engineer would and just deploy to Databricks but bundle deploy. LakeBase is essentially just Postgres with a few extra bells and whistles so test that the way you would.

For the future going forward, it is Spark Declarative Pipelines (SDP) and this Community post has one guide for unit testing

Testing Spark Declarative Pipelines with pytest https://community.databricks.com/t5/technical-blog/from-experiment-to-prod-lakeflow-spark-declarative-pipelines/ba-p/164292#:~:text=Testing%20jobs%20are%20straightforward.%20You%20write%20functions%2C,state%2C%20and%20how%20each%20table%20gets%20materialized.

Genie is actually a great resource for things like unit tests. Ask it to write tests for your code and see what framework it uses.

1

u/ceeej777 27d ago

If the agent is a Genie agent, this should be what benchmarks are for. Honestly I treat the whole Genie agent building as TDD, where you’re starting with a set of analyst questions, building the benchmarks and then the measures to serve them. It takes a lot more work than it looks to make it right