Running unit tests against Snowflake with Pytest-snowflake-bdd

214 Views Asked by At

I'm trying to figure out how to use the pip library pytest-snowflake-bdd to create bdd-style pytests against data in Snowflake. The pypi project page is here, https://pypi.org/project/pytest-snowflake-bdd/

pytest-bdd is not super familiar to me and I'm having a hard time understanding how the pytest-snowflake-bdd library needs to be used together with the normal pytest framework.

In my project I have a customer.feature and python file to run the test. I'm using the demo SNOWFLAKE_SAMPLE_DATA.TPCH_SF1.CUSTOMER table and just to test how this works, I want to check that a particular customer name exists in the table.

According to the documentation, the feature file should look like this:

Feature: Customers in SNOWFLAKE_SAMPLE_DATA

    Scenario: customers
        Given a snowflake connection
        When a temporary table called "SNOWFLAKE_SAMPLE_DATA.TPCH_SF1.CUSTOMER" has
        | C_CUSTKEY: INTEGER | C_NAME: STRING| C_ADDRESS: STRING | C_NATIONKEY: INTEGER | C_PHONE: STRING | C_ACCTBAL: INTEGER | C_MKTSEGMENT: STRING | C_COMMENT: STRING |
        | 1         | Customer#000000001 | IVhzIApeRb ot,c,E | 15 | 25-989-741-2988 | 711.56 | BUILDING | to the even, regular platelets. regular, ironic epitaphs nag e |
        | 2         | Customer#000000002 | XSTf4,NCwDVaWNe6tEgvwfmRchLXak | 13 | 23-768-687-3665 | 121.65 | AUTOMOBILE | l accounts. blithely ironic theodolites integrate boldly: caref |
        Then a sql script "./sql/example.sql" runs and the result is
        | C_CUSTKEY: INTEGER | C_NAME: STRING| C_ADDRESS: STRING | C_NATIONKEY: INTEGER | C_PHONE: STRING | C_ACCTBAL: INTEGER | C_MKTSEGMENT: STRING | C_COMMENT: STRING |
        | 1         | Customer#000000001 | IVhzIApeRb ot,c,E | 15 | 25-989-741-2988 | 711.56 | BUILDING | to the even, regular platelets. regular, ironic epitaphs nag e  | 

(The THEN step in the file is slightly odd since I don't have a .\sql\example.sql file anywhere nor does the documentation go into detail about whether I should...)

Anyway, I also have a test_for_customer.py file that looks like this:

from pytest_snowflake_bdd import scenario, given, when, then

@scenario('features/customer.feature', 'Customers in SNOWFLAKE_SAMPLE_DATA')
def test_customers():
    pass

@given('I have a customer')
def customer_name():
    cust = 'Customer#000000001'
    return cust

@when('I search for the customer')  
def search_customer(customer_name):
    cust = customer_name
    return result

@then('I should see the customer details')
def customer_details(search_customer):
    assert search_customer == 'Customer#000000001'

I run the test with the following command, passing the Snowflake connection details to it as cli arguments:

> python -m pytest --snowflake-user=PYTEST_BDD --snowflake-password=XXXX --snowflake-account myaccount.snowflakecomputing.com --snowflake-role=ROLE_PYTEST_BDD --snowflake-warehouse=WH_PYTEST

The pytest executes but no tests are ran:

================================================= test session starts =================================================
platform win32 -- Python 3.10.11, pytest-7.4.2, pluggy-1.3.0
rootdir: C:\Repos\snowflake-bdd
plugins: bdd-6.1.1, snowflake-bdd-0.2.2
collected 0 items

================================================ no tests ran in 0.04s ================================================

If anyone could shed some light on where I'm going wrong, it would be much appreciated!

0

There are 0 best solutions below