Skip to content

Instantly share code, notes, and snippets.

@sahansk2
Last active August 13, 2020 19:51
Show Gist options
  • Save sahansk2/07d374a4e96432775be69f10efb70612 to your computer and use it in GitHub Desktop.
Save sahansk2/07d374a4e96432775be69f10efb70612 to your computer and use it in GitHub Desktop.
import pytest
def read_file():
print("Running read_file")
with open("test.txt", "r") as test_file:
data = test_file.read()
print("DATA: {}".format(data if data else "[[[[EMPTY]]]]"))
return data
def write_file(data):
print("Running write_file with data: {}".format(repr(data)))
with open("test.txt", "w") as test_file:
test_file.write(data)
@pytest.fixture
def test_file(tmpdir):
print("Running test_file")
with open('test.txt', 'w'):
pass
def test_dataset_1(test_file):
print("Running test_dataset_1")
write_file("dataset_1")
read_file()
def test_dataset_2(test_file):
print("Running test_dataset_2")
data = read_file()
if data:
print("Data has content: {}".format(data))
else:
print("Data is empty")
write_file("dataset_2")
@sahansk2
Copy link
Author

sahansk2 commented Aug 13, 2020

Output:

sahan@sahan-Lenovo-YOGA-C930-ubuntu:pytest-test$ python3 -m pytest -v -s . 
================================ test session starts ================================
platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 -- /usr/bin/python3
cachedir: .pytest_cache
rootdir: /home/sahan/Documents/osai/git-gud-all/pytest-test
collected 2 items                                                                   

test_fileIO.py::test_dataset_1 Running test_file
Running test_dataset_1
Running write_file with data: 'dataset_1'
Running read_file
DATA: dataset_1
PASSED
test_fileIO.py::test_dataset_2 Running test_file
Running test_dataset_2
Running read_file
DATA: [[[[EMPTY]]]]
Data is empty
Running write_file with data: 'dataset_2'
PASSED

================================= 2 passed in 0.02s =================================

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