Showing posts with label Automation. Show all posts
Showing posts with label Automation. Show all posts

SVN commands you must know as an Automation Engineer

In the course of automation and continuous integration , you will someday come across a stage where you have to perform svn operations . And obviously, using command line (forget fancy UIs to do your business) using command line. And then you will have to start digging in,  which will take a substantial amount time ...believe me.
This happened with me recently, and I swore to myself, I cannot repeat this search and rescue operation.

The basic svn operations you must be using everyday are:

  •  Checkout 
  •  Export
  •  Add files
  •  Commit
  •  Info
  •  Update
  •  Get properties
  •  Set properties
  •  log
  •  list

Lets say, you are automating a testing activity. You basic steps must be:

  •  Get the firmware to be tested (from svn or some repo)
  •  Get test code (again from repo)
  •  Run your test
  •  Commit test results into a Tag or send a report
     For 3 of above tasks,  you are going to need to use a lot of svn commands

Export or Checkout ?

Before you start, you must decide to checkout or export your repositories. 
If you need to commit any of the files you have brought in, then you should always "checkout" . 
If you just need to run an operation , then get the result and save it to some other location or may be mail it, then just use "export" .

 Checkout an svn repo                                           

Syntax: svn checkout url destination

url is the link for the repository
destination is the location where you want to save. 

Wana checkout a particular revision?

Add a "@revision_number "at the end of the url . 
Add a -r revision_number url

Full syntax:

svn checkout -r rev_number url destination

 Export an svn repo (Download locally)                                        

Syntax: svn export url destination

  Add a local folder/file on an svn repo                                               

Syntax: svn import url destination

 Note: If the folder specified in the url doesn't exist, it will be created.

 Add files to commit                                             

Syntax: svn add file_path

file_path may be full or just file name  

"Add" here means, staging all files with changes or new files created, that we want to add/update in svn repo. 

 Commit changes                                                   

Syntax: svn commit -m "Committing changes"

-m means , message. It has to be in quotes.

This step can only be done successfully if we have added some files using "svn add" in staging area.

 Get information about the svn repo                      

Syntax: svn info 

svn info gives you lot of information , like revision number, url etc. Here is an example output.

"E:/my_repo> svn info or svn info url"

Path: .
Working Copy Root Path: E:\my_repo
Relative URL: ^/libraries/abc/products/productX/software/test/implement_exec/tools/Jenkins/trunk
Repository Root:
Repository UUID: f83c4b24-5c02-0410-a495-ade9de72abc1
Revision: 161633
Node Kind: directory
Schedule: normal
Last Changed Author: thearc31
Last Changed Rev: 159485
Last Changed Date: 2015-12-03 20:34:15 +0530 (Thu, 03 Dec 2015)

As you can see, a lot of info can be obtained through this command. 

 Update a repo                                                       

Syntax: svn update
svn update Directory_path

E.g: svn update E:/Myfolder

This is to update our local checkout folder . We should always do this before we commit .

 Get properties of a svn repo                                 

Syntax: svn propget svn:property_name

You might need to see the properties of a repo . Note that the properties start with "svn:"
There are many available. 
Let's say you want to see, what files or extensions have been ignored for a particular repo. 
So you can try:

E:\My_Branches\Trunk>svn propget svn:ignore
 As you see, it shows me that files starting with .idea have been ignored for this repository.
There are many properties available. Here's are few other popular ones:

  •  svn propget svn:externals
  •  svn propget svn:log
  •  svn propget svn:mergeinfo
This is how you can cross verify manually.

  •  Right click inside the repo directory and select "properties". A new window will popup.
  •  Click on the "Subversion" tab.

  • Click on button "Properties..." at the bottom left.
  • Note the columns "Property" and "Value".

 Set properties of a svn repo                                  

Syntax: svn propset svn:property_name property

You can set the properties of a repo through command line. You might need this if you are trying to bunch a number of links into one common folder. In that case, you can make a list of urls and set them as external properties. 

Example: This is an example of setting external properties of a repo using a text file, while has all the links you want to set as properties.

svn propset svn:externals . -F links.txt 

This is a vast topic. You can explore more on this on the svn help links.

 Get svn logs                                                          

Get latest logs if you need although, I doubt you will ever need this for automation..But then who knows. 

Syntax: svn log

 Get list of folders in svn url                                  

Get a list of all folders inside an svn url (It can be a branch or tag or a folder). It needs to be directory.

Syntax : svn list url

Art of Unittest writing : Auto-generation of unit test cases

How to auto create test cases with minimum coding 

Lets say , you want to run a series of test cases that undergo similar kind of testing  . but you need to generate different test cases for each comparison (or whatever test procedure you are using).

So imagine. We want to test a function. A function that accepts two variables, and an operator for comparison. Here's our test function:

    def _test_func(self, a, b, exp, operator):
        ans = 0
        exec("ans = %d %s %d" % (a, operator, b))
        self.assertEqual(ans, exp)

We want to test above function with various inputs....And...we want each scenario as a test case...You might need it for test report or something...

import unittest
from collections import namedtuple

class TestAuto(unittest.TestCase):
    def setUp(self):

    def tearDown(self):

# Adder function to add test case
def _add_test(cls, generator):
    for func, name, doc, a, b, exp, operator in generator():
        test = lambda self, a=a, b=b,  expected=exp, oper=operator, func=func : func(self, a, b, expected, oper)
        test.__name__ = "test_{}".format(name)
        test.__doc__ = doc
        setattr(cls, test.__name__, test)

def _test_generator_normal():
    """Generator function for testing queries"""        

    def _test_func(self, a, b, exp, operator):
        ans = 0
        exec("ans = %d %s %d" % (a, operator, b))
        self.assertEqual(ans, exp)

    tr = namedtuple('TestCase', 'name doc a b exp operator')
    test_list = (
        tr(name="sub", doc='Test for sub', a=10, b=20, exp=-10,  operator='-'),
        tr(name="add", doc='Test for add', a=30, b=50, exp=80,  operator='+'),
        tr(name="div", doc='Test for divide', a=10, b=2, exp=5, operator='/')
    for ti in test_list:
        yield _test_func,, ti.doc, ti.a, ti.b, ti.exp, ti.operator

_add_test(TestAuto, _test_generator_normal)

if __name__ == '__main__':


test_add (__main__.TestAuto)
Test for add ... ok
test_div (__main__.TestAuto)
Test for divide ... ok
test_sub (__main__.TestAuto)
Test for sub ... ok

Ran 3 tests in 0.010s


Recently , we (me and my friend Sumant)came up with one more simpler model for the same purpose.

import unittest

def generator(test_class, a, b):
    def test(self):
        self.assertEqual(a, b)
    return test

def add_test_methods(test_class):
    #First element of list is variable "a", then variable "b", then name of test case that will be used as suffix.
    test_list = [[2,3, 'one'], [5,5, 'two'], [0,0, 'three']]
    for case in test_list:
        test = generator(test_class, case[0], case[1])
        setattr(test_class, "test_%s" % case[2], test)

class TestAuto(unittest.TestCase):
    def setUp(self):
        print 'Setup'

    def tearDown(self):
        print 'TearDown'

_add_test_methods(TestAuto)  # It's better to start with underscore so it is not detected as a test itself

if __name__ == '__main__':


FAIL: test_one (__main__.TestAuto)
Traceback (most recent call last):
  File "D:/inchowar/Desktop/PyTrash/", line 5, in test
    self.assertEqual(a, b)
AssertionError: 2 != 3

Ran 3 tests in 0.019s

FAILED (failures=1)

In case , you want to be extra cautious, you can use the decorator @nottest to indicate that the function is not a test method.

"from import notttest"

Total Pageviews