Package: pytest-dbt
DBT Unit Testing with Pytest PluginThe Pytest plugin for DBT simplifies the task of writing unit tests for your DBT models, offering a straightforward and efficient testing procedure.How to InstallInstall this plugin by utilizing pip. Simply run the following command:pipinstallpytest-dbtSteps for UseTo use this plugin, follow these instructions:Begin by creating a DBT model in your usual manner.Next, executedbt compile.Now, either create a newpytest.inifile or modify an existing one. In this file, set thedbt_manifest_file_pathto point towards the appropriate manifest file. See the example below:[pytest]dbt_manifest_file_path=target/manifest.jsonThe next step is to create your tests using pytest. As an example, let's assume you have a model calledorder_sizeand you want to verify it has three columns. You can create a test filetest_order_size.pyand add the following test:customer_no_order=[{'order_id':1,"order_size":'small'},{'order_id':2,"order_size":'large'},{'order_id':3,"order_size":'medium'},{'order_id':4,"order_size":'large'},{'order_id':5,"order_size":'large'},]tables={'orders':orders,}deftest_customers_no_order():result=run_model('order_size',tables)assertassert_frame_equal(result,pd.DataFrame(customer_no_order))==NoneIncorporating User-Defined Functions (UDFs)If your databases have defined functions, you can incorporate them into the execution. For instance, if there's a modelcustomer_detailswhich employs a UDFfull_name, you can design a test filetest_customer_details.pyand append the following test:deffull_name(fname,lastname):returnfname+" "+lastnameudfs={"full_name":lambdaname,last_name:f"{name}{last_name}"}deftest_customer_details():result=run_model("customer_details",tables=tables,udfs=udfs)assertassert_frame_equal(result,pd.DataFrame(customer_details))==NoneFinally, execute the tests with the commandpytest:➜jaffle_shopgit$✗pytest=============================================================testsessionstarts=============================================================platformdarwin--Python3.8.6,pytest-7.3.1,pluggy-1.0.0
rootdir:/Users/anupkalburgi/projects/pytest-dbt/jaffle_shop
configfile:pytest.ini
plugins:dbt-0.1.1
collected5items

tests/test_customers.py.[20%]tests/test_customers_no_order.py.[40%]tests/test_order_size.py.[60%]tests/test_orders.py.[80%]tests/test_udf_customer_details.py.[100%]==============================================================5passedin0.15s==============================================================Why Not Utilize DBT Test?While DBT Test excels in conducting integration tests, it falls short when applied to unit testing.The Advantage of PytestPytest is a robust testing framework with a wide variety of plugins, backed by strong community support.Anticipated UpdatesThe following enhancements are being planned for the plugin:Inclusion of support for User-Defined Functions (UDFs).Incorporation of a cyclic redundancy check (CRC) for tracking model changes.Integration of a coverage report for SQL files.Addition of a command-line option for test configuration input, which will be particularly beneficial for Continuous Integration (CI) scenarios.Rectification of issues concerning missing files intest_dbt_manifest_file_path = dbt_manifest.json.Resolution of theAssertionError: File does not exist at path: dbt_manifest.jsonerror.