Wednesday 15 August 2012

Nose testing

For Unix like systems the nose will install by using following methods

Install nose using setuptools/distribute:
      easy_install nose
Or pip:
      pip install nose
Or, if you don’t have setuptools/distribute installed, use the download link at right to download the source package, and install it in the normal fashion:
     python setup.py install
 
After installation of the nose you can run tests for your projects by using the following commands  on terminal

     cd path/to/project       
     nosetests

The output like this
..........................................................................................................
   Ran 20 tests in 1.220s
   OK
 
For help with nosetests’ many command-line options, try:
 
   nosetests -h 
 
  -h, --help            show this help message and exit
  -V, --version         Output nose version and exit
  -p, --plugins         Output list of available plugins and exit. Combine
                        with higher verbosity for greater detail
  -v, --verbose         Be more verbose. [NOSE_VERBOSE]
  --verbosity=VERBOSITY
                        Set verbosity; --verbosity=2 is the same as -v
  -q, --quiet           Be less verbose
  -c FILES, --config=FILES
                        Load configuration from config file(s). May be
                        specified multiple times; in that case, all config
                        files will be loaded and combined
   etc

Python3

  • nose supports python3. 
  • Building from source on python3 requires distribute.
  • If you don’t have distribute installed, python3 setup.py install will install . 

Testing with nose 

 

Writing tests is easier

   nose collects tests from unittest,TestCase subclasses.
   you can also write simple test functions, as well as test classes

 

Running tests is easier

   nose collects tests automatically, as long as you follow some simple 
   guidelines for organizing your library and test code 
   Running tests is responsive

 

Setting up your test environment is easier

  nose supports fixtures at the package, module, class, and test case  level

 

 Doing what you want to do is easier

  nose comes with a number ofbuiltin plugins to help you with output capture,  
  error introspection, code coverage, doctests, and more. 

 

Developing with nose

 

Get the code

The stable branch of nose is hosted at googlecode. You should clone this branch if you’re developing a plugin or working on bug fixes for nose:
         hg clone http://python-nose.googlecode.com/hg/ nose-stable
 
The unstable branch of nose is hosted at bitbucket. You should fork this branch if you are developing new features for nose. you can clone the branch:
        hg clone http://bitbucket.org/jpellerin/nose/ nose-unstable

 




 

 

Tuesday 14 August 2012

Unit Testing

        unit testing is a method by which individual units of source code, sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures, are tested to determine if they are fit for use.Unit tests are created by programmers or occasionally by white box testers during the development process.The goal of unit testing is to isolate each part of the program and show that the individual parts are correct

 

Benefits  

 

Find problems early

Unit tests find problems early in the development cycle.Unit tests are created before the code itself is written. When the tests pass, that code is considered  complete. 

Facilitates change

Unit testing allows the programmer to re factor code at a later date, and make sure the module still works correctly.Readily available unit tests make it easy for the programmer to check whether a piece of code is still working properly.

Simplifies integration

Unit testing may reduce uncertainty in the units themselves and can be used in a bottom up testing style approach. By testing the parts of a program first and then testing the sum of its parts, integration testing becomes much easier.

Documentation

Unit testing provides a sort of living documentation of the system.Unit tests provides a basic understanding of what functionality is provided by a unit and how to use it.

Design

Each unit test can be seen as a design element specifying classes, methods, and observable behaviour. The following  example is the Testing toRoman against fromRoman
.
class SanityCheck(unittest.TestCase):        
    def testSanity(self):                    
        """fromRoman(toRoman(n))==n for all n"""
        for integer in range(1, 4000):         
            numeral = roman.toRoman(integer) 
            result = roman.fromRoman(numeral)
            self.assertEqual(integer, result) 


  This example is a test class that checks the correctness of program that converts the integer given to roman numeral and viceversa. Here the test class checks whether the given integer falls in the
 range (0,3999)


Parameterized Unit Testing (PUT)

In Parameterized Unit Tests (PUTs) , that take parameters.PUTs have been supported by JUnit 4 and various .NET test frameworks.


Unit testing limitations

Testing cannot be expected to catch every error in the program. It is impossible to evaluate every execution   path in most trivial programs.
It will not catch system errors or broader system level errors
Writing the unit tests is the difficulty of setting up realistic and useful tests.
Challenge of setting relevant initial condition so that the part of the code being tested behaves as the part of the whole system
Use of a version control system is essential for unit testing.

Applications

 

Extreme Programming

Unit testing is the cornerstone of extreme programming, which relies on an automated unit testing framework.This automated unit testing framework can be either third party, e.g., xUNIT, or created within the development group.

Techniques

Unit testing is commonly automated but may still be performed manually.

 unit testing frame work

Unit testing frameworks are most often third-party products that are not distributed as part of the compiler suite. They help simplify the process of unit testing, having been developed for a wide variety of languages.

Language-level unit testing support

Languages that directly support unit testing include:
  • C
  • Java
  • Obix
  • D

 






 


Saturday 11 August 2012

Python Coding Style

 

Python Programming Style

         Programming style is a set of rules or guidelines used when writing the source code for a computer program. It is often claimed that following a particular programming style will help programmers to read and understand source code conforming to the style, and help to avoid introducing errors.

        In python readability counts.Python community has its own standards  as that of C,C++,etc.
Python Enhancement Proposal(PEP)8 is a style guide for python which describe what a python code should look like .

 

White spaces

  • 4 spaces per indentation .
  • Never mix spaces and tabs.
  • Add a space after ',' in dictionaries and argument list and after ':' in dictionaries
  • Put spaces around assignments and comparisions.

 

Naming

  • Lower case for functions,methods,attributes
  • All caps for constants
  • _name becomes _classname__name avoid such usage as it may create problems when a subclass may need to access the same variable
  • The above situation requires either the super class or the sub class has to changed

 

Long Lines 

  • Keep lines below 79 characters
  • Use backslashes as a last resort

 

Compound Statements

  • Avoid the over use of ';' in blocks of codes,as it effect the readability of the code drastically
  • May be used at some circumstances,if this sacrifice may result in a better readability.

 

Docstrings and Comments

  • Docs string are used to specify how to use code
  • Comments Specify why and how code works
  • It is better not to specify a comment rather then a miss-leading comment.

 

Building Strings

  • Make use of ''.join() method as far as possible as it improves the performance during passing

     

Testing for Truth Values

   if x :
       print something

   Is  Better than

   if x==0 True:
      print something

 

variables

  • In python identifier are like parcel tag attached to an object.
  • Hence an identifier can becomes a tag of any object

 

Importing

  • While importing we have to import standard modules.First followed by third party modules and at last the local modules




Git Basic

Branching and Merging

 

Git enable us to maintain multiple versions of project in same working directory through branching. Initially our projects have master branch

              $ git branch   
              * master
We can see that we only have one branch ‘master’ and the ’*’ indicates that we are currently on it

Creating new branches

 

To create a new branch, we can use ‘git branch (branchname)’ which will create a branch at the point we’re currently at.
 
              $ git branch newbranch

To switch to that branch so that the work we do is saved to it instead of the ‘master’ branch, we run the ‘git checkout’ command’

                    $ git checkout newbranch 
                Switched to branch "newbranch" 
              $ git branch 
              * newbranch
                master

Now we can see that we have a new branch and that we’re on it. Now we can edit files and commit without worrying about messing up our pristine ‘master’ branch that we know works perfectly. We don’t have to share the changes we make in our ‘newbranch’ branch until we are certain they are ready.
 

Merging and removing finished branches

 

When you are done with work on a branch, you can either remove it and ignore the changes made on it if the work is not acceptable, or you can merge it into one of your long running branches.


$git checkout master 
$ git merge newbranch

Now we navigated to initial states of the project and merged changed corresponding to new branch to the initial version, master.