Created
July 13, 2020 03:22
-
-
Save Sharadh/a0e5b43e4dc5bcb1e8de566b936c5aa5 to your computer and use it in GitHub Desktop.
Code snippet to accompany blogpost on 5 pytest best practices
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
import pytest | |
def process_file(fp): | |
"""Toy function that returns an array of line lengths.""" | |
return [len(l.strip()) for l in fp.readlines()] | |
@pytest.mark.parametrize("filename, expected", [ | |
("first.txt", [3, 3, 3]), | |
("second.txt", [5, 5]), | |
]) | |
def test_antipattern(filename, expected): | |
with open("resources/" + filename) as fp: | |
assert process_file(fp) == expected | |
@pytest.mark.parametrize("contents, expected", [ | |
("foo\nbar\nbaz", [3, 3, 3]), | |
("hello\nworld", [5, 5]), | |
]) | |
def test_pattern(tmpdir, contents, expected): | |
tmp_file = tmpdir.join("testfile.txt") | |
tmp_file.write(contents) | |
with tmp_file.open() as fp: | |
assert process_file(fp) == expected |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment