Unit tests for assets and ops
Unit testing is an important tool for verifying that a computation works as intended.
While this is traditionally difficult in the context of data pipelines, Dagster helps simplify this process by allowing you to directly invoke your computations with specific input values and mocked resources to ensure that they transform data as expected.
Unit tests can't fully replace integration tests or manual review, but are capable of catching a large variety of errors with a significantly faster feedback loop.
This guide covers how to write unit tests for assets and ops with a variety of different input requirements.
Unit testing individual assets or ops is generally recommended over unit testing entire jobs.
Unit testing isn't recommended in cases where most of the business logic is encoded in an external system, such as an asset which directly invokes an external Databricks job.
Prerequisites
- Familiarity with Assets
- Familiarity with Ops and Jobs
Testing assets and ops with no arguments
The simplest assets and ops to test are those with no arguments. In these cases, you can directly invoke your definition.
- Using assets
- Using ops
Loading...
Loading...
Testing assets and ops that have upstream dependencies
If your asset or op has an upstream dependency, then you can directly pass a value for that dependency when invoking your definition.
- Using assets
- Using ops
Loading...
Loading...
Testing assets and ops with config
If your asset or op uses config, you can construct an instance of the required config object and pass it in directly.
- Using assets
- Using ops
Loading...
Loading...
Testing assets and ops with resources
If your asset or op uses a resource, it can be useful to create a mock instance of that resource to avoid interacting with external services.
- Using assets
- Using ops
Loading...
Loading...
Testing assets and ops with context
If your asset or op uses a context argument, you can use build_asset_context()
or build_op_context()
to construct a context object.
- Using assets
- Using ops
Loading...
Loading...
Testing assets and ops with a combination of parameters
If your asset or op has multiple parameters, it's recommended to use keyword arguments for clarity.
- Using assets
- Using ops
Loading...
Loading...
Next steps
- Learn more about assets in Understanding Assets
- Learn more about ops in Understanding Ops
- Learn more about config in Config
- Learn more about resources in Resources