Python Unit Test Automation


CHAPTER 6 Tips and Tricks



Download 1,71 Mb.
Pdf ko'rish
bet67/73
Sana31.12.2021
Hajmi1,71 Mb.
#213720
1   ...   63   64   65   66   67   68   69   70   ...   73
Bog'liq
2 5188215245394412341

CHAPTER 6
Tips and Tricks
In the first chapter of the book, you about learned the history and philosophy of Python. 
Subsequent chapters explored the features of various test automation frameworks in 
Python. The frameworks you explored included doctest, unittest, nose, nose2, and 
pytest. This chapter looks at coding conventions that will make the test discovery easier 
across the frameworks. Then, we will look at the concept of test-driven development and 
how it can be implemented in Python 3 projects with the help of pytest.
Coding and Filenaming Conventions for Easier 
Test Discovery
You have seen that all the xUnit-style frameworks have the feature of test discovery, 
that is, the automated detection, execution, and report generation of tests. This is a very 
important feature, as it makes life easier for code testers. You can even schedule the test 
discovery process by using OS schedulars (for example, cron in Linux-based operating 
systems and Windows Schedular in Windows), and they will run tests at scheduled times 
automatically.
In order to ensure that the test discovery system detects all the tests successfully, I 
usually follow these code and filename conventions:
•  Names of all the test modules (the test files) should start with 
test_
•  Names of all the test functions should start with test_
•  Names of all the test classes should start with Test
•  Names of all the test methods should start with test_
•  Group all the tests into test classes and packages
•  All the packages with test code should have an __init__.py file
It is always a good idea to follow the PEP 8 convention for the code. It can be found at 
https://www.python.org/dev/peps/pep-0008/
.


Chapter 6 

 tips and triCks
102
If you use these conventions for your code and filenames, the test discovery feature 
of all the test automation frameworks—including unittest, nose, nose2, and pytest—
will detect the tests without any problem. So, the next time you write your tests, follow 
these conventions for best results.
Test-Driven Development with pytest
Test-driven development (TDD) is a paradigm whereby you implement the new feature 
or requirement by writing the tests first, watch them fail, and then write the code to make 
the failed tests pass. Once the basic skeleton of the features is implemented this way, 
you then build on this by altering the tests and then changing the development code to 
accommodate the added functionality. You repeat this process as many times as needed 
to accommodate all new requirements.
Essentially, TDD is a cycle where you write the tests first, watch them fail, implement 
the required features, and repeat this process until the new features are added to the 
existing code.
By writing the automated tests before the development code, it forces you to think 
about the problem at hand first. As you start to build your tests, you have to think about 
the way you write the development code that must pass the already-written automated 
tests in order to be accepted.
Figure 
6-1
 sums up the TDD approach.
Refactor
Write a
Failing
Test
Make
the Test
Pass

Download 1,71 Mb.

Do'stlaringiz bilan baham:
1   ...   63   64   65   66   67   68   69   70   ...   73




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish