Search

Showing posts with label unit test. Show all posts
Showing posts with label unit test. Show all posts

12 June, 2019

Python unittest and exceptions: Writing custom exceptions, and catching them.


Generally in unit test, we match the responses with an expected value . But there are cases , where calling a method or passing an incorrect value (or type of value) may raise an exception . This exception might me a custom exception . Or the message or error code might me specific to the situation. So how to write test for the same.

1. Calling a method with incorrect type , raises an exception.


class MyTest(unittest.TestCase):
    def test_exception(self):
         with self.assertRaises(xmlrpclib.Fault) as cm:
            self.Proxy.setParameter('name', 1234)
            self.fail('Expected exception of type xmlrpc.Fault')


10 May, 2019

Nose test command line tricks

If you are using nose plugin to run your tests , there are some handy tricks that we can use while running tests.

Run test and generate a XML report:


nosetests --with-xunit test_folder --xunit-file=nosetests_test_results.xml

13 August, 2018

How to use Python unit test module and it's methods?

Unit test module is an in-build library in python, used to write test scripts . It gives some helpful methods while helps us designing out test in a way , that emulates a real production environment.

While writing test, we need to keep these things in mind

1. Test should start with a clean slate. Meaning the subject under test must not hold any info or be in a state, that might affect the test results.

2. Every tests should test one and only one feature . Clubbing multiple tests in one test,  is a bad idea.

Unit test provides some special methods. These can be used to handle the flow of tests as per scenario. Below is a generic template of a unit test showing all methods and their tasks.

25 June, 2018

How to test POST call using Python?


I got into a situation recently where I needed to test if my POST function was indeed working. 
    Its easy to test GET method . You just visit the URL and that's it . But for POST, you an use Plugins. But unfortunately I can't do that , because my browser (admin restrictions ) doesn't allow me to install plugins . Fortunately if you have Python , you can easily do that .