Skip to main content
Version: 1.2

Unit Testing

All the testing methods discussed so far can be run as automated unit tests using the tecton test command. This guide covers how to write and run unit tests for feature definitions in Tecton to ensure the correctness of your feature transformations.

Overviewโ€‹

Tecton supports unit testing for feature definitions to validate feature logic and configurations before deployment. Unit tests are defined in feature repositories in file paths matching the pattern **/tests/*.py.

note

Tests that exist in paths matching those specified in .tectonignore will be skipped during test execution.

Running Unit Testsโ€‹

Tests can be executed using several commands:

  • tecton test: Runs unit tests only
  • tecton apply: Runs tests and applies the repo if tests pass
  • tecton plan: Runs tests and shows changes that would be made
  • pytest: Direct pytest execution (see pytest configuration)

Prerequisites and Setupโ€‹

Installation Requirementsโ€‹

Depending on your compute mode, install Tecton with the appropriate dependencies:

For Spark:

# Pyspark 3.1
pip install 'tecton[pyspark]'

# Pyspark 3.2
pip install 'tecton[pyspark3.2]'

# Pyspark 3.3
pip install tecton pyspark==3.3

For Rift:

pip install 'tecton[rift]'

Compute-Specific Configurationsโ€‹

The compute mode (Rift, Spark) is typically determined using defaults configured on the Tecton server. However for tests running without access to the server, the compute mode can be configured using the --default-compute-mode argument. By default it is set to spark. To test with Rift, use the following command:

# Rift mode
tecton test --default-compute-mode rift

# Default (Determined by server configuration or Spark if no server access)
tecton test

Spark Session Configurationโ€‹

Configure the provided Spark session:

import pytest


@pytest.fixture(scope="module", autouse=True)
def configure_spark_session(tecton_pytest_spark_session):
tecton_pytest_spark_session.conf.set("spark.sql.session.timeZone", "UTC")

Create custom Spark session:

from importlib import resources
from pyspark.sql import SparkSession
import tecton


@pytest.fixture(scope="session")
def my_custom_spark_session():
with resources.path("tecton_spark.jars", "tecton-udfs-spark-3.jar") as path:
tecton_udf_jar_path = str(path)

spark = (
SparkSession.builder.appName("my_custom_spark_session")
.config("spark.jars", tecton_udf_jar_path)
.config("spark.driver.host", "localhost")
.config("spark.sql.session.timeZone", "UTC")
.getOrCreate()
)
try:
tecton.set_tecton_spark_session(spark)
yield spark
finally:
spark.stop()

Snowflake Configurationโ€‹

For Snowflake-based feature views:

import pytest
import snowflake.connector
import tecton
import os


@pytest.fixture(scope="module", autouse=True)
def configure_snowflake_session():
connection_parameters = {
"user": os.environ["SNOWFLAKE_USER"],
"password": os.environ["SNOWFLAKE_PASSWORD"],
"account": os.environ["SNOWFLAKE_ACCOUNT"],
"warehouse": "TECTON_WAREHOUSE",
"database": "TECTON",
"schema": "PUBLIC",
}
tecton.snowflake_context.set_connection(connection_parameters)

Validation during unit testsโ€‹

By default, Tecton objects are not validated during unit tests. If you need to run validation of Tecton objects during unit tests, set the TECTON_SKIP_OBJECT_VALIDATION configuration to False, which will require access to the Tecton API:

from tecton import conf

# Enable object validation (requires Tecton API access)
conf.set("TECTON_SKIP_OBJECT_VALIDATION", "False")

pytest Configurationโ€‹

Tests can also be run as part of a larger test suite using pytest. Just be sure to set the TECTON_OFFLINE_RETRIEVAL_COMPUTE_MODE and TECTON_BATCH_COMPUTE_MODE environment variables appropriately.

export TECTON_OFFLINE_RETRIEVAL_COMPUTE_MODE=rift
export TECTON_BATCH_COMPUTE_MODE=rift
pytest

Was this page helpful?