Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 13, 2021 12:53 pm GMT

Python UNIT Testing

UNIT Testing

It is the processing of testing whether a particular unit is working properly or not is called unit testing

Test Scenario vs Test Case vs Test Suite

  • Test Scenario consists of multiple test case for single scenario
  • So there will be different scenarios consists of test cases for a given application example of one scenario is testing login functionality.
  • Test Suite is something like grouping of test cases and executing them at a time

Implementing Python Unit testing

  • We will use Python in-built module unittest
  • We will use TestCase class to implement
  • We also use 3 instance methods setUp(), test(), tearDown()
  • Here is the simple test class
    Alt Text

  • Output of above code is
    Alt Text

  • Here we need to be careful that setUp and tearDown method names are fixed but we can change the test method name as we like bit it need to be prefix with "test" example test_sample(), test1().

  • We can include as many test methods we want into the class

    Note

  • For every test method setUp(),tearDown() methods will be executed.

  • To avoid this repeated call of setUp and tearDown methods we can use setUpClass(), tearDownClass() methods, now only one time setup and teardown methods are called regardless of how many number of testcases.

Alt Text

O/P

Alt Text

You can also use assert functions in test method

Hope you learned something!!!
Happy coding!!!


Original Link: https://dev.to/ramushetty/python-unit-testing-2b3p

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To