From dfeff7a50c73fd6a03b1969cb70063dc362c66af Mon Sep 17 00:00:00 2001 From: Fred Z Date: Tue, 14 Aug 2018 23:44:59 +0200 Subject: [PATCH] Replaces doctests test by pytest tests --- pytestdiscovering/function.py | 44 ----------------------- pytestdiscovering/test_function.py | 57 ++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 44 deletions(-) create mode 100644 pytestdiscovering/test_function.py diff --git a/pytestdiscovering/function.py b/pytestdiscovering/function.py index da58e87..d6fc421 100644 --- a/pytestdiscovering/function.py +++ b/pytestdiscovering/function.py @@ -19,50 +19,6 @@ def get_product(code, from_file=False): """ Call Open Food Facts API to get data of a single product - :Tests ONLINE: - # >>> prod_beurre = get_product('3017760000109') - # >>> prod_oreo = get_product('8410000810004') - - # >>> prod_false is False - # True - - # >>> prod_string is False - # True - - :Tests OFFLINE: - >>> prod_beurre = get_product('3017760000109', True) - >>> prod_oreo = get_product('8410000810004', True) - >>> prod_false = get_product('1664', True) - File load error : sample/product-1664.json - - >>> prod_string = get_product('string', True) - File load error : sample/product-string.json - - >>> print(prod_beurre['product_name']) - Le VĂ©ritable Petit Beurre - - >>> print(prod_beurre['nutrition_grades']) - e - - >>> print(prod_beurre['categories_tags']) - ['en:sugary-snacks', 'en:biscuits-and-cakes', \ -'en:biscuits', 'fr:petits-beurres'] - - >>> print(prod_oreo['code']) - 8410000810004 - - >>> print(prod_oreo['url']) - https://fr.openfoodfacts.org/product/8410000810004/ - - >>> print(prod_oreo['product_name']) - Biscuit Oreo - - >>> print(prod_oreo['nutrition_grades']) - e - - >>> print(prod_oreo['categories_tags']) - ['en:sugary-snacks', 'en:biscuits-and-cakes', 'en:biscuits', \ -'en:chocolate-biscuits', 'es:sandwich-cookies'] """ ERR_FILE = "File load error : {}" diff --git a/pytestdiscovering/test_function.py b/pytestdiscovering/test_function.py new file mode 100644 index 0000000..3219d13 --- /dev/null +++ b/pytestdiscovering/test_function.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +import pytest +import function as target + +@pytest.fixture() +def product_offline(): + prod_beurre = target.get_product('3017760000109', True) + prod_oreo = target.get_product('8410000810004', True) + prod_false = target.get_product('1664', True) + prod_string = target.get_product('string', True) + + return (prod_beurre, prod_oreo, prod_false, prod_string) + +@pytest.fixture() +def product_online(): + prod_beurre = target.get_product('3017760000109') + prod_oreo = target.get_product('8410000810004') + return (prod_beurre, prod_oreo) + +def test_get_product_stdout(capsys): + target.get_product('1664', True) + captured = capsys.readouterr() + assert captured.out == "File load error : sample/product-1664.json\n" + + target.get_product('string', True) + captured = capsys.readouterr() + assert captured.out == "File load error : sample/product-string.json\n" + +def test_get_product(product_offline): + # :Tests OFFLINE: + prod_beurre, prod_oreo, prod_false, prod_string = product_offline + assert prod_beurre['product_name'] == 'Le VĂ©ritable Petit Beurre' + assert prod_beurre['nutrition_grades'] == 'e' + assert prod_beurre['categories_tags'] == [ + 'en:sugary-snacks', + 'en:biscuits-and-cakes', + 'en:biscuits', + 'fr:petits-beurres' + ] + + # better use the file in sample directory + assert prod_oreo == {'categories_tags': [ + 'en:sugary-snacks', + 'en:biscuits-and-cakes', + 'en:biscuits', + 'en:chocolate-biscuits', + 'es:sandwich-cookies' + ], + 'code': '8410000810004', + 'nutrition_grades': 'e', + 'product_name': 'Biscuit Oreo', + 'url':'https://fr.openfoodfacts.org/product/8410000810004/'} + + assert not prod_false + assert not prod_string