DEV Community

Ramu Ummadishetty
Ramu Ummadishetty

Posted on

1

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!!!

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay