Monday, 23 December 2013

Print specific row from a file


File output.txt contains following records:

C:\Users\Tom\Documents\001 Career Stuff\Tests\QA_Engineer\ScriptTest\data1\data2\data3\subdir3\data-092309.xml
C:\Users\Tom\Documents\001 Career Stuff\Tests\QA_Engineer\ScriptTest\data1\data2\subdir2\data-643478.xml
C:\Users\Tom\Documents\001 Career Stuff\Tests\QA_Engineer\ScriptTest\data1\subdir1\data-4352435.xml
C:\Users\Tom\Documents\001 Career Stuff\Tests\QA_Engineer\ScriptTest\data10\subdir10\data-76532754.xml
C:\Users\Tom\Documents\001 Career Stuff\Tests\QA_Engineer\ScriptTest\data10\subdir10\subdir9\data-6327789928.xml
C:\Users\Tom\Documents\001 Career Stuff\Tests\QA_Engineer\ScriptTest\data5\data6\subdir6\data-467368.xml
C:\Users\Tom\Documents\001 Career Stuff\Tests\QA_Engineer\ScriptTest\data5\subdir5\data-231324.xml
C:\Users\Tom\Documents\001 Career Stuff\Tests\QA_Engineer\ScriptTest\data7\subdir7\data-98765.xml
-> Done


User wants to print specific rows only. Follow the procedure below to do that:

>>>f = open('output.txt')
>>> lines = f.readlines()
>>> print(lines[2]) # prints 3rd row of the file
C:\Users\Tom\Documents\001 Career Stuff\Tests\QA_Engineer\ScriptTest\data1\subdir1\data-4352435.xml
>>>
>>> #Do this when output needs to be in quotation marks (+ without "\n")
>>> print(repr(lines[2].strip('\n'))) # repr() manages quotation marks
'C:\\Users\\Tom\\Documents\\001 Career Stuff\\Tests\\QA_Engineer\\ScriptTest\\data1\\subdir1\\data-4352435.xml'

Follow the link to see more:
http://stackoverflow.com/questions/2081836/reading-specific-lines-only-python

Sunday, 24 November 2013

How to open text file in Python

Example 1
This example done on simple text file "test.txt" having content as per following:

x
y
z

Do following in Python Shell in order to read file content:

>>> # open the file in a read mode
>>> f = open (r"C:\opt\test.txt")
>>> # once file is open, read all the lines
>>> f.readlines()

...and as a result you get list with items representing lines in the text file.

>>>
['x\n', 'y\n', 'z']
>>>

Note: 
Characters "\n" represent end of the line. In order to remove it, use function "rstrip("\n")".




Example 2
This example is done in the same file as in Example 1, let's try another option, which is reading line by line via for loop:

...in Python Shell:

>>> f = "C:\opt\test.txt"
>>> #
>>> # open the file in a read mode
>>> f_open = open(file, 'r')
>>> #
>>> # and read line by line
>>> for lines in f_open:
      # remove end characters "\n" on each line
      lines = lines.rstrip("\n")
      print(lines)

Results are as following (this time not a list):

>>>
x
y
z
>>>

Saturday, 23 November 2013

Count occurence of character in a string

String = syzygy
I am interested in how many times letter "y" occurs in the string.

Just write this in Python Shell:

>>> str.count('syzygy', 'y')
3

or

>>> 'syzygy'.count('y')
3

Testing Automatically using Doctest

Follow the procedure below:

1) Create function in IDE (e.g. convert_to_celsius)

def convert_to_celsius(fahrenheit):
    '''(number) -> float

    Return the number of Celsius degrees equivalent to fahrenheit degrees

    >>> convert_to_celsius(32)
    0.0
    >>> convert_to_celsius(212)
    100.0
    '''
    return (fahrenheit - 32) * 5 / 9

def colder_temperature(temp1, temp2):
    '''(number, number) -> number
    Return the colder of the two temperatures, temp1 (degrees Celsius)
    and temp2 (degrees Fahrenheit), in degrees Celsius.
    '''
    return

2) Save the module and run it in IDE (press F5)
3) Go to Python Shell
4) Do following:

>>> import doctest
>>> doctest.testmod()
TestResults(failed=0, attempted=2)
>>>
>>>#--> If falied=0, then the function is correct

Sunday, 11 August 2013

Test Management Tools

ReQtest

  • Easy to learn, no need to pay for training
  • No need to install the software, login and password is enough
  • Watch the video below to see more
  • Click here to see company website

Monday, 1 July 2013