GaelGil / sentence-generator

A website that generates a new sentence from some given text using markov chains
https://sentence-gen.herokuapp.com/
0 stars 0 forks source link

how to improve test using @pytest.mark.parametrize and @pytest.fixture #10

Open GaelGil opened 5 years ago

GaelGil commented 5 years ago

@pytest.mark.parametrize and @pytest.fixture are used to avoid repetition while writing test for example

@pytest.mark.parametrize("earned,spent,expected", [
    (30, 10, 20),
    (20, 2, 18),
])
def test_transactions(my_wallet, earned, spent, expected):
    my_wallet.add_cash(earned)
    my_wallet.spend_cash(spent)
    assert my_wallet.balance == expected

This test uses @pytest.mark.parametrize and makes it more efficent to test more scenarios. What are some test I could try and write using @pytest.mark.parametrize and @pytest.fixture to stop some repetition.

camoverride commented 5 years ago

Here's an example of a test you can write.

from app import clean_data

@pytest.mark.parametrize("uncleaned_data, cleaned_data", [
    ("the! man", ["the", "man"]),
    ("this; and He said:", ["this", "and", "he", "said"]),
    ("I looooove lasagna ;)", ["i", "looooove", "lasagna"]),
    ("I h8 lasagna ;)", ["i", "h", "lasagna"]),
    ("1 + 1 = 2", []),
])
def test_clean_data(uncleaned_data, cleaned_data):
    assert clean_data(uncleaned_data)[0] == cleaned_data

Also, notice that this test: