Skip to content

Instantly share code, notes, and snippets.

@vperron
Forked from lionel-panhaleux/quizz.py
Created September 5, 2019 15:23
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 vperron/273981cd7642728185852abfccd52e9a to your computer and use it in GitHub Desktop.
Save vperron/273981cd7642728185852abfccd52e9a to your computer and use it in GitHub Desktop.
Python test
#!/usr/bin/env python
# Write a function that determines if any of its arguments evaluates to True.
def test_find_true():
"""
>>> find_true(True, {})
True
>>> find_true(None, (), 0)
False
"""
# Write a function that returns a list of even integers from 0 to n inclusive.
def test_even_integers():
"""
>>> even_integers(3)
[0, 2]
"""
# Write a function that counts how many times each item occurs in an iterable,
# and displays a list in the format shown below, sorted by decreasing count.
def test_show_items():
"""
>>> show_items(['spam', 'eggs', 'eggs'] * 7)
14 eggs
7 spam
"""
# Write a function that extracts the country code, phone number and extension
# from a string formatted as: +<country code>.<phone number>[x<extension>]
# - <country code> contains up to 3 digits;
# - <phone number> contains up to 14 digits;
# - <extension> is optional and contains up to 40 digits.
def test_parse_phone_number(number):
"""
>>> parse_phone_number('+33.158186740')
('33', '158186740', None)
>>> parse_phone_number('+33.158186740x0930')
('33', '158186740', '0930')
>>> parse_phone_number('42')
Traceback (most recent call last):
...
ValueError: '42' is not a valid phone number
>>> parse_phone_number('+33.158186740xabc')
Traceback (most recent call last):
...
ValueError: '+33.158186740xabc' is not a valid phone number
"""
# Write a class that can be instanciated with any keyword arguments,
# and saves them as instance variables.
def test_magic_class():
"""
>>> x = MagicClass(spam=42)
>>> x.spam
42
>>> x.eggs
Traceback (most recent call last):
...
AttributeError: 'MagicClass' object has no attribute 'eggs'
"""
import doctest
doctest.testmod()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment