Created
August 1, 2019 14:16
-
-
Save lionel-panhaleux/e4c0079d28a252ae7ebe8008ebf7788d to your computer and use it in GitHub Desktop.
Python test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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