Skip to content

Instantly share code, notes, and snippets.

@obestwalter
Last active August 4, 2019 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save obestwalter/14eb9d6176f0253125215558cbf87427 to your computer and use it in GitHub Desktop.
Save obestwalter/14eb9d6176f0253125215558cbf87427 to your computer and use it in GitHub Desktop.
Solution for Euler 1 one the pytest way
"""Solve https://projecteuler.net/problem=1 the pytest way
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
"""
import pytest
@pytest.fixture(name="key", scope="session")
def euler_cache_provider(request):
key = "e1"
request.config.cache.set(key, 0)
yield key
result = request.config.cache.get(key, None)
assert 0, result
@pytest.mark.parametrize("i", list(range(1, 1000)))
def test_euler_1(i, request, key):
result = request.config.cache.get(key, 0)
if i % 3 == 0 or i % 5 == 0:
request.config.cache.set(key, result + i)
@obestwalter
Copy link
Author

This is the expected result - 999 green tests but at teardown is an assertion error that prints out the result. I know. Stupid :)

pytest test_euler_1.py                                                   
================================= test session starts =================================
platform linux -- Python 3.7.4, pytest-5.0.1, py-1.8.0, pluggy-0.12.0
rootdir: /home/ob/do/ob-python-materials
collected 999 items                                                                   

test_euler_1.py ............................................................... [  6%]
............................................................................... [ 14%]
............................................................................... [ 22%]
............................................................................... [ 30%]
............................................................................... [ 37%]
............................................................................... [ 45%]
............................................................................... [ 53%]
............................................................................... [ 61%]
............................................................................... [ 69%]
............................................................................... [ 77%]
............................................................................... [ 85%]
............................................................................... [ 93%]
...................................................................E            [100%]

======================================= ERRORS ========================================
_______________________ ERROR at teardown of test_euler_1[999] ________________________

request = <SubRequest 'key' for <Function test_euler_1[1]>>

    @pytest.fixture(name="key", scope="session")
    def euler_cache_provider(request):
        key = "e1"
        request.config.cache.set(key, 0)
    
        yield key
    
        result = request.config.cache.get(key, None)
>       assert 0, result
E       AssertionError: 233168
E       assert 0

test_euler_1.py:12: AssertionError
========================= 999 passed, 1 error in 1.31 seconds =========================

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment