9
Jul

Remove Punctuation From String

Question:

Write a python program to remove punctuations from a string

Program:

Explanation:

There are fourteen punctuation marks commonly used in English grammar. They are the period, question mark, exclamation point, comma, semicolon, colon, dash, hyphen, parentheses, brackets, braces, apostrophe, quotation marks, and ellipsis.

Output:

Do share your feedback in comments to help us adding more programs like this.

Checkout more Python Programs

Click here
9
Jul

Addition of Two Matrices

Question:

Write a python program to add two matrices

Program:

Explanation:

If A and B are the two matrices for addition, both of them must be of the same order.

If i and j are the index positions, then result[i][j] = A[i][j] + B[i][j]

Output:

Do share your feedback in comments to help us adding more programs like this.

Checkout more Python Programs

Click here
9
Jul

HCF Python Program

Question:

Write a python program to find the HCF

Program:

Explanation:

The Highest Common Factor (H.C.F) of two (or more) numbers is the largest number that divides evenly into both numbers. In other words the H.C.F is the largest of all the common factors. The common factors or of 12 and 18 are 1, 2, 3 and 6. The largest common factor is 6, so this is the H.C.F. of 12 and 18.

Output:

Do share your feedback in comments to help us adding more programs like this.

Checkout more Python Programs

Click here
9
Jul

Factorial Using Recursion

Question :
Write a program to find the factorial of a number using recursive function

Program:

Explanation:
Factorial of a number is the product of all natural numbers till that number. It does not exist for negative numbers.

For example, the factorial of 4 would be 1*2*3*4=24

A recursive function is a function that calls on itself. In the above recursive function, the function calls on itself with n-1 as argument till n becomes 1 and 1 is returned. Each time the function is called, product the corresponding n and the factorial of n-1 is returned. Hence, factorial is found.

Output:
1.

2.
3.
Do share your feedback in comments to help us adding more programs like this.

Checkout more Python Programs

Click here
9
Jul

Factors of a Number

Question:

Write a python program to find the factors of a given number

Program:

Explanation:

The factors of a number are any numbers that divide into it exactly. This includes 1 and the number itself. For example, the factors of 6 are 1, 2, 3 and 6. The factors of 8 are 1, 2, 4 and 8. For larger numbers it is sometimes easier to ‘pair’ the factors by writing them as multiplications.

Output:

Do share your feedback in comments to help us adding more programs like this.

Checkout more Python Programs

Click here
9
Jul

Largest of Three Numbers

Question

Write a python program to find the largest of three numbers

Hint: Use if and elif

Program:

 

Explanation:

Three numbers are accepted from the user ‘nos1′,’nos2′,’nos3’.

First ‘nos1’ is checked.If it is greater than both ‘nos2’ and ‘nos3’, then ‘large’ is allocated wth the value of ‘nos1’

Otherwise ‘nos2’ is checked. If it is greater than both ‘nos1’ and ‘nos2’,then ‘large’ is allocated with the value of ‘nos2’

Otherwise ‘large’ is allocated with ‘nos3’

Output:

Do share your feedback in comments to help us adding more programs like this.

Checkout more Python Programs

Click here
9
Jul

LCM of Two Numbers

Question:

Program to find the LCM of two numbers

Program:

Explanation:

A common multiple is a number that is a multiple of two or more numbers. The common multiples of 3 and 4 are 0, 12, 24, …. The least common multiple(LCM) of two numbers is the smallest number (not zero) that is a multiple of both.

Output:

Do share your feedback in comments to help us adding more programs like this.

Checkout more Python Programs

Click here
9
Jul

The Palindrome Program

Question 

Write a python program to check whether given string is palindrome or not without using any inbuilt function to reverse the string

Program:

Explanation:

A palindrome is a string which is same read forward or backwards. Dad is a palindrome while Python is not.

In the program, we first accept the string to ‘string’. Since capital letters and small letters are perceived differently because of difference in ASCII codes, we convert the string to all lower for uniformity using inbuilt function ‘x.lower()’ where x is the string

In the above program, we have used the slice operator to reverese the string.The slice operator[n:m:p] returns the part of the string from nth to the (m-1)th character Setting p to -1 returns the reverse order of the slice of string you asked for. Since here we want the whole string, we use the shorthand [::-1]

Compare both the string and it’s reversal. If they are equal, then it’s a palindrome, otherwise not.

Output:

1.

2.
Do share your feedback in comments to help us adding more programs like this.

Checkout more Python Programs

Click here
9
Jul

Python Armstrong program

Question 2

Write a Python program to check whether a given number is armstrong or not.

Program:

Explanation:

Armstrong number is the number whose sum of cube of individual digits is equal to the number itself.

Output:

1.

2.
Do share your feedback in comments to help us adding more programs like this.

Checkout more Python Programs

Click here
9
Jul

The Fibonacci Sequence

Question:

Program to print the Fibonacci Number

Program:

Explanation:

A Fibonacci sequence is the integer sequence of 0, 1, 1, 2, 3, 5, 8….

The first two terms are 0 and 1. All other terms are obtained by adding the preceding two terms

Ex: 5th term is sum of 4th and 3rd term;3=2+1

Output:

1.

2.
3.
Do share your feedback in comments to help us adding more programs like this.

Checkout more Python Programs

Click here
9
Jul

Multiplication Table

Question;

Write a program to print the multiplication table of a number

Program:

Output:
Do share your feedback in comments to help us adding more programs like this.

Checkout more Python Programs

Click here
1
Jul

Print Python Built-In Function Documentation

Question:
Write a program to print some Python built-in functions documents, such as abs(), int(), raw_input(). The built-in document method is __doc__.

Program:

Explanation:

Python documentation strings (or docstrings) provide a convenient way of associating  documentation  with  Python  modules, functions, classes, and methods. An object’s docsting is defined by including a string constant as the first statement in the object’s definition

Output:

Do share your feedback in comments to help us adding more programs like this.

Checkout more Python Programs

Click here
19
Jun

How to Debug Code with Android Studio

How to Debug Code with Android Studio



Being Tester we are used to reporting bugs and we know that developer debug the code to find out the line of code causing the bug. But now you are the one coding the test cases and you are responsible for solving any bug in your testing framework.


Android Studio provides some shortcut keys for debugging. In this post, we will use our HelloWord Espresso test Case Example and We will run our test Case In Debug mode and we will debug our code line by line.


It is always good practice to use Debug Mode for root tracing the bug as it allows you to run your code line by line by setting a debug point. Debug point is the nothing but marking the line of code from which you wish to start debugging. This speeds up your work and efficiency.

  • In your Android Studio project copy the below test case.
  •  
  • Now we will set a debug pointer. Suppose we are confident that our test of code is working fine except our test and we wish to debug the test case then we will place our debug pointer at the first line inside our test case. For this click on the left side space in front of the selected line and the pointer is set as shown the below screenshot(Red Dot represents the debug point).

  • Now we need to run our test case in Debug mode. Select the class name -> right click -> Debug as “MyActivityTest”. Make sure that the Android device is attached to your computer with USB debugging ON.

    Debug

  • As soon as your code runs till the debug point. You will see a debug console on your bottom left side and the code stops running at the debug point. Now the control is in your hands to run the code line by line.

  • The button highlighted in below screenshot allows you to run the rest of the code. When you click on it rest of the code runs automatically.

  • The button highlighted in below screenshot allows you to run the code step by step. Each time you click the button the line highlighted in the code gets executed.

  • You can also observe what is getting stored in your variables by opening checking variable window –

This will help you debug your test cases and write a efficient and bug free code.






18
Jun

How to Get Code Coverage with Jacoco and Espresso

How to Get Code Coverage with Jacoco and Espresso


Finding code coverage is very important for testing because it allows you to measure your testing efforts. If you have a test suite prepared for your application but you are not sure test suite covers how much percentage of code then finding the code coverage will be good practice Once you get the coverage report, you will know exactly what part of code is not covered in your test case and then you can add test cases to cover the code.

Jacoco is java code coverage tool which allows you to calculate code coverage for your unit tests and instrumented tests. Jacoco can be used with Ant, Maven & Gradle build tools. Today, I will show you step by step integration of Jacoco with Gradle for Espresso test suite.


I have done this setup for Simple Hello world project to give idea of how Jacoco works and gives proper code coverage report.

Pre-requisite –

  • Android Studio
  • Junit
  • Espresso Setup
  • Espresso Test Case written as per our previous post for “Hello Word”

How to Setup Jacoco and Get Coverage ?

    • Create an android project as shown in my previous post.
    • Create a java class inside src->main->androidTest and write your Espresso test case in the class
    • Create a new class named AndroidJococoTestRunner in src -> main ->androidTest and copy below code inside the class.
    • Open app-> build.gradle and copy below code in defaultConfig.
 

    • Then click on Gradle on right side of Android studio -> click on refresh -> other -> right click on createDebugAndroidTestCoverageReport -> click on run

 

    • This action will run all the test cases inside src->main->androidTest and give the report in app->build-> reports ->coverage -> debug.
    • pen index.html file in the browser to check the code coverage report as shown in below screenshot.

Jacoco_Code_Coverage1

 

 






17
Jun

How to Write and Run First Test Case in Android Studio?

How to Write and Run First Test Case in Android Studio?


 

So far we have learnt on creating our Android Studio project and overview of Junit and TestNG testing framework. In this Tutorial, we will write our first test case and run it. It sounds exciting but before that we need to decide a testing tool which we want to use for writing testing Framework.

We have many famous testing tools in market like Appium, Robotium , Espresso etc. Espresso is an open source framework provided by Android Studio so let use that to write our first test case.

The main purpose of this tutorial is to learn to write and run test case in Android Studio so we will focus on this and not on testing tools. We will learn about testing tools in future posts.

Perquisite –

  • Create an Android Studio Project
  • Configure TestNG Framework.
  • Configure Espresso Tool

 

Steps for Writing Test Cases –

      • Go to app -> src ->androidTest -> Java Android Studio

 

      • Right click on the package name

 

      • Go to New -> Java Class and click
      • Enter File Name and Click on Ok.
      • “Now your java class is ready.>

Now we will write a simple code as per given below

Test Case Explanation –

1. The class uses two annotations @LargeTest and @Runwith these two annotations are used to specify the behavior for our TestClass.
2. The class extends AcitivtyInstrumentationTestCase2 class with launch activity as MainActivity which we want to test in this example.
3. @Before and @After is annotations and used to specify which method should be executed before each test case and which method should be executed after completion of each test case.
4.@Test annotation is added for each test case method.
5. In the below test case the getAcitivity() launches our Main Activity.
6. onView() is used to select a view for testing and withId() is used to locate the UI element and rest is used to check whether the particular element is displayed or not.

Running Test Case –

        • For Android Studio Version Less than 2.1
          • Click on Build Variants
      • Select Android Instrumentation Tests
      • Right click on class name and click on Run
      • For Android Studio Versions More than 2.1
        1. Right click on class name and click on Run





16
Jun

How to Configure TestNG in Android Studio ?

How to Configure TestNG in Android Studio?


 

In our previous tutorial, we have learnt about Junit Testing framework and now in this we will get to know about one more powerful testing framework called TestNG. TestNG framework as some different functionalities which makes it more powerful testing framework..

What is TestNG Framework?

TestNG is a testing framework developed from inspiration of Junit and Nunit with additional features making is more effective and simple for use. It is a powerful automation testing framework where NG Stands for Next Generation. It gives developers power to write more flexible and powerful test cases.

Features of TestNG –

  • It allows you to use new features of Java programming language while writing test cases.
  • Like Junit it provides annotations to identify your test methods and flow of execution.
  • Separates compile time test code and run time configurations.
  • Provides powerful test case execution support.
  • Allows you to perform multithreading testing.
  • Initially It was designed for unit testing but now allows load testing , parallel testing etc.

There is so much more in this and we will explore more and more when we start coding. But it is very important to learn basics about Testing frameworks before writing your test cases as selection of right framework is like building pillar for your automation framework.

How to Setup TestNG –

    • Create and Android Project or open your existing project

 

    • Go to app -> build.gradle

 

  • Copy the below line of code in your dependencies section.
     
  • Click on Sync
  • Once you get build successful message then you are ready to go.

 






15
Jun

How to Configure Junit Testing Framework in Android Studio

How to Configure Junit Testing Framework in Android Studio?


In our previous post, we talked about the Android Studio and Java Installation. In this we will learn about very famous unit testing framework setup for android studio that is Junit. Before jumping on to the setup let us learn more about Junit Testing Framework.

What is Junit ?

Junit is a widely used testing framework used for Java Programming language. Before you learn, how to write automated test cases it is very important to learn Junit testing framework in detail. It helps you to define the flow of your testing framework.
Junit is built on idea of “first testing and then coding” which helps in increasing productivity of the test case developer and stability of the code.

 

Features of Junit Testing –

  • It is open source testing framework allowing users to write and run test cases effectively.
  • Provides various types of annotations to identify test methods.
  • Provides different types of Assertions to verify the results of test case execution.
  • It also gives test runners for running tests effectively.
  • It is very simple and hence saves time.
  • It provides ways to organize your test cases in form of test suits
  •  You can integrate Junit with Eclipse, Android Studio, Intellij etc.

How to Setup JUnit–

    • Create a new Project in Android Studio – (Refer our previous post)
      • Go to app -> build.gradle
      • In the file add below line of code in dependencies section:
        • testCompile group: ‘junit’, name: ‘junit’, version: ‘4.12’

     

      • Click on SyncJunit
      • Once you get Build is Successful message then your Junit Framework is Set and you can use it while writing your test cases
      • Note – We will see how to use Junit in your test cases in our future posts

     






14
Jun

How to install Android Studio?

How to install Android Studio?


 

A Manual tester want to start and learn automation, there are many curiosity come to their mind about what tool to use?, which IDE(Integrated Development Environment) to select?, what testing framework to select? This tutorial will help you to know about an IDE called Android Studio. IDE is nothing but development environment for writing and executing code.
Android Studio is the official IDE for coding android app, but it is also very good alternative for writing and executing test cases. Android Studio is very easy to install and faster to write, deploy and run.

 

Let us see why Android Studio is best suitable for testing

  • Android Studio supports both JUnit and TestNG testing framework which are most commonly used for building automation framework.
  • We can use various automation tool with Android Studio like Appium, Espresso and Robotium etc.
  • Android Studio provides support for Unit Testing (White box testing) and Instrumentation Testing (Black box testing).
  • After Test case execution, Android Studio generates html report as an overview of passed and failed test cases.
  • Most interesting feature provided by Android Studio is we can calculate code coverage on the basis of test cases written. This helps us measure our testing efforts.

After knowing, how we can effectively use Android Studio for our testing effort. Now, we will see how to install Android Studio. Before installing Android Studio, it is required to install Java.

      • First of all Download latest JDK version from This Link
      • Click on “Accept License Agreement” and download latest code.
      • Click on .exe file and then java installation window will open.
      • Click on next and finish installation.
      • Installation doesn’t complete unless you set JAVA_HOME.
      • So go to my computer properties.
      • Click on Advanced System Settings -> Advanced > Environment Variables
      • Click on New under System Variable. Then in Variable Name enter “JAVA_HOME” and Variable Value as “Java Installed Path” (It mostly lies in : C:\Program Files\Java\jdk1.8***)
      • Now, edit path variable and add “;%JAVA_HOME%\bin” at the end.
      • To verify that java is installed successfully, go to command prompt and enter “java – version”. You will see “Java Version 1.8.***
    • Now Download Android Studio from THIS LINK
    • Click on .exe and install the Android SDK into your system.
    • Installation doesn’t complete unless you set ANDROID_HOME. So go to my computer properties.
    • Click on Advanced System Settings -> Advanced -> Environment Variables -> New Variable and Variable Name – “ANDROID_HOME” and variable value-” C:\Users A\Downloads\android-sdk . The same way as we did for Java Home
    • Then, Select Path Variable -> Edit and then add following path after semicolon (;) %ANDROID_HOME%/tools;%ANDROID_HOME%/platform-tools

 






13
Jun

Difference between unit tests and instrumentation tests

Difference Between Unit Tests and Instrumentation Tests


 Android Studio allows you to write unit test as well as instrumentation tests. It is very important for us to learn more about unit tests and instrumentation tests. We have defined it in terms of Android Application.

In layman terms, Unit tests are the test cases written for testing code written by developers and Instrumentation test are the test cases written for testing UI & functionality of the application. In short, Unit tests is associated with white box testing and instrumentation tests is associated with black box testing.

Unit Tests –

  • Unit tests runs in local JVM and minimizes the execution time.
  • Unit Tests cannot test UI of the app without mocking activity objects.
  • Unit test are used for white box testing for testing code.
  • Most of the time Unit Tests are written by Developers.
  • You don’t need a device connected for execution of Unit Tests.

Instrumentation Tests –

  • Instrumentation tests are used for black box testing.
  • It is used to test GUI of the application along with its functionality in real environment.
  • For execution of Instrumentation tests you need a device /emulator on which the application is first installed and then tests are executed.
  • Automation Testers are involved in writing instrumentation framework.





11
Jun

Basics of Android Project Structure

Basics of Android Project Structure


Coding with android studio looks very promising because it is an IDE based on IntelliJ IDEA that is used for android application development. Even though we are not going to build android application , it is very important for us to understand the project structure so that we can code effective and understandable automation test framework.

There are five important components of Android Project Structure like main,gradle,.idea, app and External Libraries. Let us Discuss this in detail.

  • Main Project –This would be entire project context. Whatever you do in IntelliJ IDEA, you do that in the context of a project. A project is an organizational unit that represents a complete software solution. A project in Android Studio is like a workspace . In android Studio a project, can contain multiple modules. This means that, in theory it is possible to build multiple apps within the same project.
  • .idea –.idea is used for storing project specific metadata in Android Studio.
  • Project Module –This is the actual project folder where your application code resides. The application folder has following sub directories
    • build: This has all the complete output of the make process i.e. classes.dex, compiled classes and resources, etc.
    • libs : The lib folder is used for storing project specific jar files..
    • src: The src folder can have both application code and android unit test script. You will find two folders named “androidTest” and “main” correspond to src folder. The main folder contains two subfolders java and res. The java folder contains all the java codes and res contains drawables, layouts, etc.
  • Gradle – Gradle is used for configuration of build procress. We will see more detais about this in our nest post.
  • External Libraries -This is a place where Referenced Libraries and information on targeted platform SDK is shown.