I'm Kevin
🐍
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.
Debugging period (1947–1956)
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
no, not really
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
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
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
Unit and behavioral testing should complement each other