Hi, can we run the dbt test when running the dbt-a...
# integration-dbt
j
Hi, can we run the dbt test when running the dbt-asset?
dagster bot responded by community 1
g
Hey Julius! I set up dbt test assets using an asset factory like below:
Copy code
def test_asset_factory(
    model: str, non_argument_deps: set[AssetKey] | set[str] | None
) -> AssetsDefinition:
    @asset(
        name=f"test_{model}",
        non_argument_deps=non_argument_deps,
        required_resource_keys={"dbt"},
        group_name="dbt_tests",
        key_prefix="dbt_project",
        compute_kind="dbt",
    )
    def _asset(context: OpExecutionContext) -> DbtCliOutput:
        res = context.resources.dbt.test(models=[model])
        return res

    return _asset


test_assets = [
    test_asset_factory(**kwargs)
    for kwargs in [
        ...
    ]
]
This is for a dbt CLI resource - not sure how this goes with dbt cloud
You could probably adapt this to run on all of your dbt assets you’ve imported by iterating over the asset keys object, rather than defining a list of kwargs
b
Some commands have a parameter
use_build_command
to use
dbt build
instead of
dbt run
g
ah TIL thanks, Benoit daggy love
j
@Benoit Perigaud I'm using the
use_build_command
, but for some reason, the jobs don't catch the dbt-test if it is a failure test. I also want whole job fails when dbt-test fail.
n
Hi! Thanks for the information! I have a question. Do the sensors interfere in any way with use_build_command? we use a sensor for executions
b
On my side, the project I have behave as expected with
build
, if a test fails, the dbt command will skip the downstream model and Dagster will report an error.
j
@Benoit Perigaud ok, thanks for the information, let me try more with
use_build_command
. Did you use the lastest version of dbt-dagster?
@Nicolas Luchetti Yes, I'm also using the sensor to trigger the dbt job to run
b
I use it on a personal project (yes, dbt and dagster in my spare time 🙈), that doesn’t fail often but I got it failing as expected when I was on dagster 1.2 and dbt 1.4
🌈 2
n
Thanks @Julius ! And the
use_build_command
do you include it in the job.py?
j
@Nicolas Luchetti yes, include it in the job.py. The dbt-run-job does run dbt-test when all the test pass, but when I set the test fail (on purpose), it skips the dbt-test and the jobs still mark as success. let me investigate more with the
use_build_command
n
Thank @Julius! If I manage to solve it I will let you know too.
j
thanks a lot @Nicolas Luchetti!
🙌 2