Unittest – Unit Testing Framework (Python)

Unit testing : It refers to the kind of testing where the tester refers to a small software module at a time and testing it in an isolated fashion.

Unittest supports test automation, sharing of setup and shutdown code for tests, aggregation of tests into collections, and independence of the tests from the reporting framework module.This module provides classes that make it easy to support these qualities for a set of tests.

Test Fixture : A test fixture is setting up well known and fixed environment in which tests are run so that to get a particular/expected  outcome.

A test is generally done in four phases.

Four Phases of a test are :

  • set up – It is used for setting up test fixture.
  • Exercise – interact with system under test.
  • verify – determine whether the expected outcome has been obtained.
  • Tear down – for cleanup of test fixture so that it returns to original state.

Note : Unit testing is a kind of white box testing.

Here I am giving a brief introduction  to unitttesting (python) so that those who wants to get started with software testing will find it interesting and easier.

I have taken a simple example of  a calculator application and will try to test it through unit testing frame work provided by unittest module in python.

# calculator.py

class Calculator:
def add(self, x, y):
return x + y

def sub(self, x, y):
return x - y

def mul(self, x, y):
return x * y

def div(self, x, y):
assert (y != 0)
        return x / y

The following is the unittest code for testing this calculator application.


# test_calculator.py

import unittest
from calculator import Calculator

class TestCalculator(unittest.TestCase):
def setUp(self):
self.cal = Calculator()

def test_add(self):
res = self.cal.add(10, 2)
self.assertEqual(12, res)

def test_sub(self):
res = self.cal.sub(7, 4)
self.assertEqual(3, res)

def test_mul(self):
res = self.cal.mul(5, 25)
self.assertEqual(125, res)

def test_div(self):
res = self.cal.div(20, 4)
self.assertEqual(5, res)

if __name__ == '__main__':
unittest.main()

In unittest framework we have to create a subclass of unittest TestCase as I have create in the above code. Here TestCalculator is the subclass of unittest TestCase class. The important thing to note here is that the name of the testcase class start with the word Test(i.e should follow this pattern ‘ Test*’ ) and the test methods name follows the pattern ‘ test* ‘. The methods test_add, test_sub, test_mul, test_div is used for testing add(), sub() , mul(), div() methods of Calculator class.Then setUp() method is used to create test fixture for the tests.

Skipping tests and Expected failures:

unittest provides methods for skipping test methods as well as test class and to mark a test as expected failures when we know that the test is going to fail.Here is an example code showing test skipping and expected failures.

# test_calculator.py

import unittest
from calculator import Calculator

class TestCalculator(unittest.TestCase):
def setUp(self):
self.cal = Calculator()

def test_add(self):
res = self.cal.add(10, 2)
self.assertEqual(12, res)

@unittest.skip("Demonstrating method skippping")
def test_sub(self):
res = self.cal.sub(7, 4)
self.assertEqual(12, res)

    @unittest.skipIf(2 > 0, "Demonstrating method skipping using skipIf")
def test_mul(self):
res = self.cal.mul(5, 25)
self.assertEqual(125, res)

@unittest.expectedFailure
def test_div(self):
res = self.cal.div(20, 4)
self.assertEqual(5, res)

if __name__ == '__main__':
unittest.main()

Get CPU information in Linux

It’s quite easy to get CPU information in Linux.Just open terminal and give the following command:

$ cat /proc/cpuinfo

Note : Presence of lm(Long mode) in flags indicate that the CPU is 64 bit whereas its absence indicates that it’s a 32 bit processor.

You can also use uname -i command  to know whether its a 32 bit or a 64 bit processor.

$ uname -i

It will say “i686” for 32bit and “x86_64” for 64bit.

Mounting a Pendrive

Manually Mounting a Pendrive :

At first whenever you encounter a Problem with USB devices,first check the latest debug information generated from kernel just after you plugin your device and/or just after you encounter the problem by using cmd as
$dmesg

Now run
$lsub                                                                                                              //to see list of usb devices
then
$sudo fdisk -l                                                                                                    //to see all attached storage devices and their partition

$sudo mkdir /media/external                                                                                      //creating mount point

$sudo mount -t vfat /dev/sdb1 /media/external -o uid=1000,gid=1000,utf8,dmask=027,fmask=137                     //The options following the “-o” allow your user to have ownership of the drive, and the masks allow for extra security for file system permissions. If you don’t use those extra options you may not be able to read and write the drive with your regular username.
or otherwise if the device is formatted with NTFS, run:

sudo mount -t ntfs-3g /dev/sdb1 /media/external

Troubleshooting:
1.Use ‘dmesg’ cmd to get necessary details.

2.Check for User Priviledge:
Go to System->Administration->User and Groups, choose the user, click on “Properties”, then go to the “User Privileges” tab. You should have the “Access external storage devices automatically” option checked.

3.Check Preferences:
If your usb device doesn’t appear on your desktop, you should check that the automount action is enabled in the preferences:
Navigate to “System” > “Preferences” > “Removable Drives and Media”
Verify that all “Mount removable drives when…” are checked.

Resetting My SQL password

For resetting mysql root passwd follow the following steps:

Step 1: Stop mysql service

/etc/init.d/mysql stop

Step 2: Start to mysql server w/o password

mysqld_safe –skip-grant-tables &

Step 3: Now connect to mysql server using mysql client

mysql -u root

Step 4: Setup new mysql root user password

mysql> use mysql;
     mysql> update user set password=PASSWORD(“sctfmysql”) where User=’root’;
     mysql> flush privileges;
     mysql> quit

Step 4: Stop mysql Server

/etc/init.d/mysql stop

Step 5:Start mysql server and test it

/etc/init.d/mysql start

mysql -u root -p

Step 6:It’s done 😀

Resetting root password in Linux using Backtrack

backtrack

Steps for resetting password of root to ‘sctfroot’ using backtrack.

Step1: Boot the backtrack cd and log in as root.

Step2: find the partition in which the linux is installed using cmd
$fdisk-l                                        //we are already loged in as root hence its not required to use sudo cmd anywhere.

Step3: suppose linux is installed in partition /dev/sda6 then make a directory name sda6 in mnt directory and mount that partition to it using following cmd(s)
$mkdir /mnt/sda6                             //make sda6 in mnt directory  
$mount /dev/sda6 /mnt/sda6

Step4: cd /mnt/sda6                                  //entering in sda6

Step4: change the permission of passwd file to 755
$chmod 755 passwd                             //check comments in step2

Step5: open passwd file
$vi /etc/passwd

Step6: Find root and delete the whatever is there in between the first ‘:’ and second ‘:’ and save the file.It will make the root password less.
root:x:0:0:root:/root:/bin/bash                //before

root::0:0:root:/root:/bin/bash               //after 

Step7: Now remove backtrack and restart the computer and open in grub mode(recovery mode) and you will find the root prompting at cmd line without asking for password.Now use passwd cmd to change passwd of root to sctfroot.

$passwd root
and type password as ‘sctfroot’ when it is asked.

Note: You can prevent him from hacking your sytem by setting a Bios password(so that he can’t access your system without cracking your bios password 😀 ).