์ฝ๋ ํ์ง์ ์ ์งํ๊ธฐ ์ํด ์ฌ์ฉํ ์ฌ๋ฌ ๊ฐ์ง ๋๊ตฌ์ ๋ํด์ ์ ๋ฆฌํ๋ค.
์ฌ์ฉํ ์ฝ๋ ํ์ง ๋๊ตฌ
- Sonar : ์ฝ๋ ํ์ง ๋ถ์, ๋ณด์ ์ทจ์ฝ์ ํ์ง
- Black : ์ฝ๋ ์คํ์ผ ์๋ ํฌ๋งทํ
- Flake8 : ์ ์ ์ฝ๋ ๋ถ์ (PEP 8, ๋ฌธ๋ฒ ์ค๋ฅ ๊ฒ์ฌ)
- isort : import ๋ฌธ ์๋ ์ ๋ ฌ
- pytest : Python ํ ์คํธ ํ๋ ์ ์ํฌ
1๏ธโฃ Sonar (SonarQube / SonarCloud)
- Sonar๋ ์ฝ๋ ํ์ง์ ๋ถ์ํ๋ ์ ์ ์ฝ๋ ๋ถ์ ๋๊ตฌ
- SonarQube, SonarCloud ๋ ๊ฐ์ง ํํ๋ก ์ ๊ณต
โ ์ฌ์ฉ ๋ฐฉ๋ฒ
- ๋ก์ปฌ์์ SonarQube๋ฅผ ์ค์นํ๊ณ ์คํ (sonarqube + sonar-scanner ์ฌ์ฉ)
- CI/CD ํ๊ฒฝ์์๋ SonarCloud๋ฅผ ์ด์ฉํ์ฌ ๋ถ์
sonar-scanner \
-Dsonar.projectKey=my_project \
-Dsonar.organization=my_org \
-Dsonar.host.url=https://sonarcloud.io \
-Dsonar.login=my_sonar_token
2๏ธโฃ Black
- Python ์ฝ๋ formatter
- ์ฝ๋ ์คํ์ผ ์๋ ์ ๋ฆฌ
โ ์ฌ์ฉ ๋ฐฉ๋ฒ
1. ํจํค์ง ์ค์น
pip install black
2. ์ฝ๋ ํฌ๋งทํ ์คํ
black my_script.py
3. ํน์ ํด๋์ ์๋ ๋ชจ๋ ํ์ผ ํฌ๋งทํ
black .
3๏ธโฃ Flack8
Python์ ์ ์ ์ฝ๋ ๋ถ์ ๋๊ตฌ โ ์ฝ๋ ์คํ์ผ, ๋ฌธ๋ฒ ์ค๋ฅ๋ฅผ ์ฒดํฌํ๋ ์ญํ
โ ์ฌ์ฉ ๋ฐฉ๋ฒ
1. ํจํค์ง ์ค์น
pip install flake8
2. ์ฝ๋ ๊ฒ์ฌ ์คํ
flake8 my_script.py
3. ํน์ ํด๋ ๊ฒ์ฌ
flake8 my_project/
โ .flake8 ํ์ผ์ ๋ง๋ค์ด ํน์ ๊ท์น ๋ฌด์ ๊ฐ๋ฅ
[flake8]
max-line-length = 100
ignore = E203, E266, E501
4๏ธโฃ isort
Python import ์์ ์๋ ์ ๋ฆฌ
โ ์ฌ์ฉ ๋ฐฉ๋ฒ
1. ํจํค์ง ์ค์น
pip install isort
2. ํน์ ํ์ผ ์ ๋ฆฌ
isort my_script.py
3. ํน์ ํด๋ ๊ฒ์ฌ
isort .
5๏ธโฃ pytest
Python ํ ์คํธ ํ๋ ์ ์ํฌ โ ๋จ์ ํ ์คํธ, ๊ธฐ๋ฅ ํ ์คํธ ์คํํ๋๋ฐ ์ฌ์ฉ
- assert ํค์๋ ์ฌ์ฉ
- @pytest.mark.parametrize๋ก ํ ์คํธ ๋ฐ์ดํฐํ ๊ฐ๋ฅ
- pytest.fixture๋ก ํ ์คํธ ํ๊ฒฝ ๊ตฌ์ฑ ๊ฐ๋ฅ
- ํ ์คํธ ์คํ ๊ฒฐ๊ณผ ๋ฆฌํฌํธ ์ง์
- ๋ณ๋ ฌ ํ ์คํธ ์คํ ๊ฐ๋ฅ (pytest-xdist ํ์ฉ)
โ ์ฌ์ฉ ๋ฐฉ๋ฒ
1. ํจํค์ง ์ค์น
pip install pytest
2. ๊ฐ๋จํ ํ ์คํธ ์์ฑ
def add(a, b):
return a + b
def test_add():
assert add(2, 3) == 5
3. ํ ์คํธ ์คํ
pytest
โ ํ์ฌ ๋๋ ํ ๋ฆฌ์ ํ์ ๋๋ ํ ๋ฆฌ์์ ๋ชจ๋ test_*.py ๋ฐ *_test.py ํ์ผ์ ์ฐพ์ ์คํ
โ ํ๋ก์ ํธ ์ ์ฒด์ ํ ์คํธ๋ฅผ ์คํํ ๋ ์ฌ์ฉ !!
4. ํน์ ํ์ผ์ ํ ์คํธ ์คํ
pytest test_example.py
5. ํน์ ํ ์คํธ ํจ์๋ง ์คํ
pytest test_example.py::test_add
โ test_example.py ํ์ผ ์์ ์๋ test_add ํจ์๋ง ์คํ
6. ์คํจํ ํ ์คํธ๋ง ๋ค์ ์คํ
pytest --lf
7. ํ ์คํธ ์์ธ ๋ฆฌํฌํธ ์ถ๋ ฅ
pytest -v
โ Git pre-commit hook ์ค์
Git์ pre-commit hook์ ์ค์ ํ๋ฉด, black, flake8, isort, pytest ๊ฐ์ ๋๊ตฌ๋ฅผ ์ฝ๋๋ฅผ ์ปค๋ฐํ๊ธฐ ์ ์ ์๋์ผ๋ก ์คํํ ์ ์๋ค. ์ด๋ฅผ ํตํด ์ฝ๋ ์คํ์ผ์ ์ ์งํ๊ณ , ๋ฒ๊ทธ๋ฅผ ๋ฏธ๋ฆฌ ๋ฐฉ์ง ๊ฐ๋ฅ
โ ๋์ ๋ฐฉ์
- git commit -m "๋ฉ์์ง" ์ ๋ ฅ
- pre-commit์ด ์คํ๋๋ฉด์ black, flake8, isort, pytest ์ฒดํฌ
- ์ค๋ฅ ๋ฐ์ ์ ์ปค๋ฐ์ด ์ทจ์๋จ
- ๋ชจ๋ ๊ฒ์ฌ ํต๊ณผ ์ ์ปค๋ฐ ์ฑ๊ณต !
โ ์ค์ ๋ฐฉ๋ฒ
1. pre-commit ํจํค์ง ์ค์น
pip install pre-commit
2. pre-commit-config.yaml ํ์ผ ์์ฑ
- ํ๋ก์ ํธ์ ๋ฃจํธ ๋๋ ํ ๋ฆฌ์ .pre-commit-config.yaml ํ์ผ์ ๋ง๋ค๊ณ ๋ด์ฉ ์ถ๊ฐ
- ํ์ผ ์์:
repos:
- repo: https://github.com/psf/black
rev: 23.7.0 # ์ต์ ๋ฒ์ ํ์ธ ํ ์
๋ฐ์ดํธ ๊ฐ๋ฅ
hooks:
- id: black
- repo: https://github.com/PyCQA/flake8
rev: 6.1.0 # ์ต์ ๋ฒ์ ํ์ธ ํ ์
๋ฐ์ดํธ ๊ฐ๋ฅ
hooks:
- id: flake8
- repo: https://github.com/PyCQA/isort
rev: 5.12.0 # ์ต์ ๋ฒ์ ํ์ธ ํ ์
๋ฐ์ดํธ ๊ฐ๋ฅ
hooks:
- id: isort
args: ["--profile", "black"] # black ์คํ์ผ๊ณผ ํธํ๋๋๋ก ์ค์
- repo: https://github.com/pytest-dev/pytest
rev: 7.4.2 # ์ต์ ๋ฒ์ ํ์ธ ํ ์
๋ฐ์ดํธ ๊ฐ๋ฅ
hooks:
- id: pytest
3. Git Hook ํ์ฑํ (pre-commit install)
- ์ด์ pre-commit์ Git์ pre-commit hook์ผ๋ก ๋ฑ๋ก
pre-commit install
โ ํ ์คํธ ์คํ
์ค์ ํ์ธ์ ์ํด ์๋์ผ๋ก ์คํ ๊ฐ๋ฅ
pre-commit run --all-files
'๐ฉ๐ปโ๐ป' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[MQTT] pc 2๊ฐ ํต์ (python, ์๋ฐฉํฅ ํต์ ) (0) | 2025.02.25 |
---|---|
[MQTT] Mac mosquitto ์ค์น / ์คํ / ์ค์ง (0) | 2025.02.25 |
nginx์ mTLS ์ธ์ฆ์ ๋ฐ๊ธํ๊ณ ๊ฒ์ฆํ๊ธฐ (0) | 2024.08.16 |