Python Unit Test Automation



Download 1,71 Mb.
Pdf ko'rish
bet26/73
Sana31.12.2021
Hajmi1,71 Mb.
#213720
1   ...   22   23   24   25   26   27   28   29   ...   73
Bog'liq
2 5188215245394412341

Listing 3-7.  test_module04.py
import unittest
import inspect
class TestClass04(unittest.TestCase):


Chapter 3 

 Unittest
41
        def test_case01(self):
                print("\nClassname : " + self.__class__.__name__)
                print("Running Test Method : " + inspect.stack()[0][3])
class TestClass05(unittest.TestCase):
        def test_case01(self):
                print("\nClassname : " + self.__class__.__name__)
                print("Running Test Method : " + inspect.stack()[0][3])
if __name__ == '__main__':
        unittest.main(verbosity=2)
You can run the entire test module with the following command:
python3 -m unittest -v test_module04
The output is as follows:
test_case01 (test_module04.TestClass04) ...
Classname : TestClass04
Running Test Method : test_case01
ok
test_case01 (test_module04.TestClass05) ...
Classname : TestClass05
Running Test Method : test_case01
ok
----------------------------------------------------------------------
Ran 2 tests in 0.090s
OK
You can run a single test class with the following command:
python3 -m unittest -v test_module04.TestClass04
The output is as follows:
test_case01 (test_module04.TestClass04) ...
Classname : TestClass04
Running Test Method : test_case01
ok
----------------------------------------------------------------------
Ran 1 test in 0.077s
OK


Chapter 3 

 Unittest
42
You can also run a single test case with the following command:
python3 -m unittest -v test_module04.TestClass05.test_case01
The output is as follows:
test_case01 (test_module04.TestClass05) ...
Classname : TestClass05
Running Test Method : test_case01
ok
----------------------------------------------------------------------
Ran 1 test in 0.077s
OK
This way you can control the granularity of the test execution.
Listing All the Command-Line Options and Help
You can list all the command line options of unittest using the -h command-line option. 
Run the following command:
python3 -m unittest -h
The following is the output:
usage: python3 -m unittest [-h] [-v] [-q] [-f] [-c] [-b] [tests [tests ...]]
positional arguments:
  tests           a list of any number of test modules, classes and test
                  methods.
optional arguments:
  -h, --help      show this help message and exit
  -v, --verbose   Verbose output
  -q, --quiet     Quiet output
  -f, --failfast  Stop on first fail or error
  -c, --catch     Catch ctrl-C and display results so far
  -b, --buffer    Buffer stdout and stderr during tests
Examples:
  python3 -m unittest test_module               - run tests from test_module
  python3 -m unittest module.TestClass          -  run tests from module.
TestClass
  python3 -m unittest module.Class.test_method  - run specified test method


Chapter 3 

 Unittest
43
usage: python3 -m unittest discover [-h] [-v] [-q] [-f] [-c] [-b] [-s START]
                                    [-p PATTERN] [-t TOP]
optional arguments:
  -h, --help            show this help message and exit
  -v, --verbose         Verbose output
  -q, --quiet           Quiet output
  -f, --failfast        Stop on first fail or error
  -c, --catch           Catch ctrl-C and display results so far
  -b, --buffer          Buffer stdout and stderr during tests
  -s START, --start-directory START
                        Directory to start discovery ('.' default)
  -p PATTERN, --pattern PATTERN
                        Pattern to match tests ('test*.py' default)
  -t TOP, --top-level-directory TOP
                        Top level directory of project (defaults to start
                        directory)
For test discovery all test modules must be importable from the top level
directory of the project.
This way you get a detailed summary of the various command-line options available 
with unittest.
Important Command-Line Options
Let’s take a look at the important command-line options in unittest. Take a look at the 
code in Listing 
3-8
 for example.

Download 1,71 Mb.

Do'stlaringiz bilan baham:
1   ...   22   23   24   25   26   27   28   29   ...   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