Python Crash Course: 챕터 11
- Testing code with pytest library
- 1. Python includes a tool called pip that’s used to install third-
party packages. Because pip helps install packages from
external resources, it’s updated often to address potential
security issues. So update pip first (open new terminal window and type the code below)
python -m pip install --upgrade pip
python -m pip install --upgrade 패키지_이름
2. Now install pytest
python -m pip install --user pytest
If there's difficulty, remove --user and then re-enter the code in terminal
- unit test and test caseA unit test verifies that one specific aspect of a function’s behavior is
correct. A test case is a collection of unit tests that together
prove that a function behaves as it’s supposed to
def get_formatted_name(first, last):
"""Generate a neatly formatted full name."""
full_name = f"{first} {last}"
return full_name.title()
from name_function import get_formatted_name
def test_first_last_name():
"""Do names like 'Janis Joplin' work?"""
formatted_name = get_formatted_name('janis', 'joplin')
assert formatted_name == 'Janis Joplin'
1. create a test file with the name test_ involved
2. import function you want to test from the file
3. always add test_ to the function being tested
*If you are testing multiple functions, make sure those test functions have different unique names
4. change the name of the function you want to test, test
function names should be long enough that if you see the
function name in a test report, you’ll have a good sense of
what behavior was being tested
5. An assertion is a claim about a condition. Here we’re claiming that the value of
formatted_name should be 'Janis Joplin'.
6. Open the folder that contains _test files that need to be tested. Navigate to the terminal (CTRL+ `) in the editor window and type pytest to run testing
7. Specify the directory or the file name that need to be tested. For example,
pytest C:\Users\ANITA\Desktop\chapter_10\writing_to_a_file
8. If there's an error from the pytest, fix the code from the original file, not the test file
- Testing Classes
- Using a fixture to test a class
import pytest
from survey import AnonymousSurvey
@pytest.fixture
def language_survey():
"""A survey that will be available to all test functions."""
question = "What language did you first learn to speak?"
language_survey = AnonymousSurvey(question)
return language_survey
def test_store_single_response(language_survey):
"""Test that a single response is stored properly."""
language_survey.store_response('English')
assert 'English' in language_survey.responses
def test_store_three_responses(language_survey):
"""Test that three individual responses are stored properly."""
responses = ['English', 'Spanish', 'Mandarin']
for response in responses:
language_survey.store_response(response)
for response in responses:
assert response in language_survey.responses
-
- 1. import pytest
- 2. write decorator @pytest.fixture
- 3. apply the decorator @pytest.fixture to the mother method defined
- 4. write the mother mothod's variable (that returns value) as a parameter in all following methods to be tested