student.py
import json
class StudentDB:
def __init__(self):
self.__data = None
def connect(self, data_file):
with open(data_file) as json_file:
self.__data = json.loads(data_file)
def get_data(self, name):
for student in self.__data['students']:
if student['name'] == name:
return student
test_student.py
from student import StudentDB
import pytest
# Fixture is more like a replacement for writing both setup and teardown
# If not fixture, you can write separate functions for setup_module, teardown_module
# You need to pass db to all the unit test functions
#If you not pass scope=module, this fixture is called for each and every unit test
@pytest.fixture(scope='module')
def db:
print('-------Inside setup-------')
db = StudentDB()
db.connect('data.json')
yield db
print('-------Inside teardown-------') # Since this is module level, this is called at the end of module
db.close()
def test_scott_data(db): # Passed from above texture
scott_data = db.get_data('Scott')
assert scott_data['id'] == 1
assert scott_data['name'] == 'Scott'
assert scott_data['result'] == 'Pass'
def test_mark_data(db): # Passed from above texture
scott_data = db.get_data('Mark')
assert scott_data['id'] == 2
assert scott_data['name'] == 'Mark'
assert scott_data['result'] == 'Fail'
No comments:
Post a Comment