pytest 설치
테스트 코드를 작성하고 실행할 수 있도록 pytest를 설치한다.
pip install -U pytest간단히 hello_test.py를 만들어 보자.
def test_hello():
assert hello('JOKER') == 'Hello, JOKER!'pytest를 실행하면 해당 프로젝트의 *_test.py 파일 안에 있는 모든 test_* 테스트 함수를 확인하게 된다.
pytest간단히 통과시키자.
def hello(name):
return 'Hello, {}!'.format(name)
def test_hello():
assert hello('JOKER') == 'Hello, JOKER!'파일이 수정될 때마다 자동으로 실행하게 하려면 pytest-watch를 쓰면 된다.
pip install -U pytest-watch실행할 때는 오히려 더 짧게 쓰면 된다.
ptwpylama 설치
올바르게 코딩하는 걸 도울 수 있도록 정적 분석기를 사용하자. 여기서는 Pylava를 이용해 검사한다.
pip install -U pylava간단히 돌려보자.
pylavavenv 폴더가 있다면 그것도 포함해서 검사하기 때문에 지나치게 오래 걸린다. --skip 플래그로 해당 폴더를 제외하자.
pylava --skip "venv/*"Pylint도 함께 사용해 보자. Pylava의 기본 Linter 목록은 여기에서 확인할 수 있다.
# Pylint 설치
pip install -U pylava-pylint
# Linter 목록 바꿔서 실행
pylava --skip "venv/*" --linters "pycodestyle,pyflakes,mccabe,pylint"매번 linters 플래그를 적어주는 게 불편하다면 pylava.ini 또는 pytest.ini 파일을 만들어서 다음 내용을 넣어준다.
[pylava]
skip = venv/*
linters = pycodestyle,pyflakes,mccabe,pylintdocstring이 빠졌고 빈 줄이 부족하다고 하니 모두 추가하자.
"""Sample test code."""
def hello(name):
"""Return greeting message."""
return 'Hello, {}!'.format(name)
def test_hello():
"""hello function test."""
assert hello('JOKER') == 'Hello, JOKER!'pytest와 pylava를 통합해 보자.
pytest --pylava매번 --pylava 플래그를 입력하는 게 불편하면 pytest.ini 파일에 다음을 추가한다.
[pytest]
addopts = --pylava이제 python-watch 하나만 실행하면 코드가 올바른지 계속 감시할 수 있다.
ptw'app > python' 카테고리의 다른 글
| python datetime / date (0) | 2019.06.04 |
|---|---|
| pycharm 프로젝트 시작시 venv 셋팅 (0) | 2019.01.22 |
| ubuntu pycharm에서 ImportError: No module named pip에러가 난다면 (0) | 2016.07.18 |
| 혹시 모든 mysql관련 라이블러리를 설치해도 ImportError: No module named 'MySQLdb' 가 나온다면! (0) | 2016.07.01 |
| mysql-python install error 'x86_64-linux-gnu-gcc' (0) | 2016.06.30 |