Behave yourself!

A better way to do testing

Hi!

I'm Kevin

🐍

Agenda

What is testing in software?

Why is it important?

Unit testing

Is unit testing perfect?

​A more 'complete' approach: Behavioral testing

Gherkin!

How to implement it

When to use it

Some useful resources

Conclusions

What is testing in software?

Testing is the process of finding errors in a software program to make sure it is working the way it is intended.

testing through history

Debugging period (1947–1956)

why is testing important?

types of testing

  • Manual testing
  • Automated testing
  • Continuous testing

types of testing

  • Manual testing
  • Automated testing
  • Continuous testing

types of testing

  • Manual testing
  • Automated testing
    • ​unit testing
  • Continuous testing

types of testing

  • Manual testing
  • Automated testing
    • ​unit testing
    • behavior tests
  • Continuous testing

unit testing

unit testing

import pytest
from ..product import Product
from ..product.exceptions import WrongTaxField


@pytest.mark.parametrize("tax", [15, 20])
def test_basic_fields(tax):
    '''
    Creates a new product and validates the name is title case
    and the new price is the expected
    '''
    product_name = "the product title"
    base_price = 100
    new_product = Product(product_name, base_price, tax)
    assert new_product.name == product_name.title()
    assert new_product.price == base_price + base_price*(tax/100)

unit test using pytest example

is it a perfect approach?

no, not really

a more 'complete' approach

behavioral testing

behavioral testing

Feature: Stock management
    A site where you save your products

    Scenario: Adding new products to the stock
        Given We have a stock with 3 products

        When I add 3 to the stock

        Then I should now have 6 products

Gherkin example

How do you implement it?

How do you implement it?

Pytest BDD

Behave

from pytest_bdd import scenario, given, when, then


@scenario('test.feature', 'BDD')
def test_init():
    pass

@given('we have pytest-bdd installed')
def step_impl(*args, **kwargs):
    pass

@when('we implement a test')
def step_impl(*args, **kwargs):
    pass

@then('pytest-bdd will test it for us!')
def step_impl(*args, **kwargs):
    assert
from behave import given, when, then, context

@given('we have behave installed')
def step_impl(context):
    pass

@when('we implement a test')
def step_impl(context):
    pass

@then('behave will test it for us!')
def step_impl(context):
    pass

How do you implement it?

Pytest BDD

from pytest_bdd import given, when, then, scenario, parsers
from ...utils.stock import Stock
from ...product import Product


@scenario(
    "../features/stock.feature",
    "Adding new products to the stock",
)
def test_add_products():
    pass


@given(parsers.parse("We have a stock with {products:d} products"), target_fixture="stock")
def stock(products, add_products_to_stock):
    stock = Stock('The stock')
    add_products_to_stock(stock, products)
    return stock


@when(parsers.parse("I add {extra_products:d} to the stock"))
def add_stock(stock, extra_products, add_products_to_stock):
    add_products_to_stock(stock, extra_products)


@then(parsers.parse("I should now have {new_count:d} products"))
def new_stock_number(stock, new_count):
    assert len(stock) == new_count

When to use behavioral testing?

Useful resources

Conclusions

Unit and behavioral testing should complement each other

  • If you want to implement behavioral testing you need to think about a test that will verify a whole expected behavior and not a small piece or method
  • Implement behavioral testing for important flows, not everything

Conclusions

Thank you!

Behave yourself! A better way to do testing

By Kevin Hernández

Behave yourself! A better way to do testing

  • 1,304