Comment on page
⚗️ Test İşlemleri
Python üzerinde pytest ve modülleri ile test işlemleri
setup.py
setup.cfg
src/
mypkg/
__init__.py
app.py
view.py
tests/
__init__.py
foo/
__init__.py
test_view.py
bar/
__init__.py
test_view.py
- 📃 Test yapılandırma dosyası olarak
setup.cfg
dosyası kullanılır - 💡
[<modül>]
şeklinde modüle özgü ayarlar tanımlanır - 🧪
pytest
için[tool:pytest]
alanı altında test yapılandırması yapılır - 👀
pytest
'in bakacağı dizinlerpython_files
vetestpaths
alanında belirtilir
[flake8]
max-line-length = 99
max-complexity = 11
exclude = */migrations/*
[options]
# tests_require is a list of dependencies that are *absolutely required*
# to run the tests. tests_require is used when running tests from your
# *current* Python environment (that is, not using tox).
# tests_require is ignored by tox.
#
# As such, you can usually get away with neglecting tests_require ---
# it's not a big deal if some of the dependencies get left out.
#
# If you're running tests from your current environment, it's because
# you're actively developing, in which case you usually have an
# environment you built for development. But if you have to change
# environments mid-development for any reason, tests_require can save you
# from getting tripped up.
#
# tests_require is used when running tests and debugging through an IDE like
# PyCharm, to ensure the environment the IDE is using has the requirements.
#
# Unless you're in one of those situations, you can simply ignore this.
tests_require = pytest
[aliases]
# Alias `setup.py test` to `setup.py pytest`
test = pytest
[tool:pytest]
# If a pytest section is found in one of the possible config files
# (pytest.ini, tox.ini or setup.cfg), then pytest will not look for any others,
# so if you add a pytest config section elsewhere,
# you will need to delete this section from setup.cfg.
norecursedirs =
.git
.tox
.env
venv
dist
build
migrations
python_files =
test_*.py
*_test.py
tests.py
addopts =
-ra
--strict
--ignore=docs/conf.py
--ignore=setup.py
--ignore=ci
--ignore=.eggs
--doctest-modules
--doctest-glob=\*.md
--tb=short
--pyargs
# The order of these options matters. testpaths comes after addopts so that
# nameless in testpaths is interpreted as
# --pyargs nameless.
# Any tests in the src/ directory (that is, tests installed with the package)
# can be run by any user with pytest --pyargs nameless.
# Packages that are sensitive to the host machine, most famously NumPy,
# include tests with the installed package so that any user can check
# at any time that everything is working properly.
# If you do choose to make installable tests, this will run the installed
# tests as they are actually installed (same principle as when we ensure that
# we always test the installed version of the package).
# If you have no need for this (and your src/ directory is very large),
# you can save a few milliseconds on testing by telling pytest not to search
# the src/ directory by removing
# --pyargs and nameless from the options here.
testpaths =
src/ypackage
tests/
- 🖤 Test komutu
python -m pytest
olarak bilinir - 🕵️♂️
pytest
tüm test paketlerinisetup.cfg
dosyasında belirtildiği şekilde bulacaktır - 🧐
flake8 --exclude=venv* --statistics
komutu ile kod kalitesini ölçebilirsiniz
📢 Sadece
pytest
komutu hatalara sebebiyet vermekte- 📦 Unit test için
unittest
paketi kullanılır - 💠
setUp
metodu ile her test öncesi işlemler tanımlanır - 💦
tearDown
metodu ile de her test sonrası işlemler tanımlanır - 👨💼
python -m pytest
komutu ile çalıştırılabilirler
from unittest import TestCase
class TestStringMethods(TestCase):
def setUp(self):
self.p = subprocess.Popen('notepad')
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')
def test_isupper(self):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper())
def test_split(self):
s = 'hello world'
self.assertEqual(s.split(), ['hello', 'world'])
# check that s.split fails when the separator is not a string
with self.assertRaises(TypeError):
s.split(2)
def tearDown(self):
self.p.terminate()
if __name__ == '__main__':
unittest.main()
- 👮♂️ Beklenen hata mesajları için
with
yapısı kullanılır - 🧐 Beklediğin hata tipini
with
içerisinde yazıp, alt scope'una kodlarını yazın - 💁♂️ Eğer içeride hata fırlatılmazsa
pytest
test başarısız olarak gösterecektir
with pytest.raises(subprocess.SubprocessError):
subprocess.check_output(
"ygitbookintegration {}".format(args),
universal_newlines=True
)
- 📑 PyDoc alanında
>>>
karakterleri ile kod yazabiliriz - 👁️ Kod çıktılarını hemen alt satıra yazılır
- 👮♂️ String çıktıları için
'
karakteri arasına yazmanız gerekir - 🚀 Harici fonksiyonlar da kullansa, doküman ile test edilebilir