Skip to content

Instantly share code, notes, and snippets.

@jdevera
Created October 30, 2018 14:30
Show Gist options
  • Save jdevera/6e5aed88ad7a5d159df68efb2c581f69 to your computer and use it in GitHub Desktop.
Save jdevera/6e5aed88ad7a5d159df68efb2c581f69 to your computer and use it in GitHub Desktop.
Example of Pytest parameterization of tests
import re
import pytest
def camel_to_snake(string):
s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', string)
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()
CASES = [
('ISeeALittle', 'i_see_a_little'),
('iSeeALittle', 'i_see_a_little'),
('NotInventedHere', 'not_invented_here'),
('notInventedHere', 'not_invented_here'),
('JKRowling', 'j_k_rowling'),
('URRight', 'u_r_right')
]
@pytest.mark.parametrize("orig,expected", CASES)
def test(orig, expected):
assert camel_to_snake(orig) == expected
@jdevera
Copy link
Author

jdevera commented Oct 30, 2018

test/test_chicken.py::test[ISeeALittle-i_see_a_little] PASSED        [ 16%]
test/test_chicken.py::test[iSeeALittle-i_see_a_little] PASSED        [ 33%]
test/test_chicken.py::test[NotInventedHere-not_invented_here] PASSED [ 50%]
test/test_chicken.py::test[notInventedHere-not_invented_here] PASSED [ 66%]
test/test_chicken.py::test[JKRowling-j_k_rowling] FAILED             [ 83%]
test/test_chicken.py::test[URRight-u_r_right] FAILED                 [100%]

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