Skip to content

Instantly share code, notes, and snippets.

@stephen-bunn
Created May 6, 2019 20:09
Show Gist options
  • Save stephen-bunn/b33e00d06a6413213b3da9d83542a413 to your computer and use it in GitHub Desktop.
Save stephen-bunn/b33e00d06a6413213b3da9d83542a413 to your computer and use it in GitHub Desktop.
Medium - Pytest example tests for is_plaindrome
"""Basic pytest for ``is_palindrome`` method."""
from typing import List
import pytest
def is_palindrome(string: str) -> bool:
"""Determine if a given string is a palindrome.
:param str string: The string to process
:return: True if given string is a palindrome, otherwise False
:rtype: bool
"""
return string == string[::-1]
@pytest.fixture
def palindromes() -> List[str]:
"""Pytest fixture which returns a list of palindromes.
:return: A list of palindrome strings.
:rtype: List[str]
"""
return ["", "a", "aba", "ababa", "racecar"]
@pytest.fixture
def not_palindromes() -> List[str]:
"""Pytest fixture which returns a list of non-palindromes.
:return: A list of non-palindrome strings.
:rtype: List[str]
"""
return ["ab", "abab", "abaa", "bbaa"]
def test_is_plaindrome(palindromes: List[str]):
"""Test true cases for ``is_palindrome``.
:param List[str] palindromes: A list of palindrome strings
"""
for value in palindromes:
assert is_palindrome(value)
def test_not_is_palindrome(not_palindromes: List[str]):
"""Test false cases for ``is_plaindrome``.
:param List[str] not_palindromes: A list of non-palindrome strings
"""
for value in not_palindromes:
assert not is_palindrome(value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment