Airo Global Software

Think Beyond Future !

Application Testing

Application testing refers to the process of testing any software application using scripts, tools, or any test automation frameworks in order to identify errors.

enter image description here

What is a PHP unit?

PHPUnit is one of the most well-known and optimized unit testing packages of PHP. It is the most relevant choice of many engineers for rectifying various developmental loopholes of the app. Its main action is to perform overall unit testing for apps, therefore, you can test your code every minute. Yet you can also use it for different actions, as it is more flexible to do just more than unit testing. Moreover, it carries all major PHP frameworks including Laravel. Many expert programmers recommend it for all Laravel unit testing processes, because of its optimized standards.

PHPUnit is created with simple assertions, which make it pretty easy for you to test your code successfully. Further, it gives great results when you are testing code components personally, giving you exotic results, so that mistakes can be identified easily. However, this means that testing much more advanced components like controllers, models and form validations can be a bit difficult as well.

How is unit testing with laravel possible?

Laravel is the most popular PHP framework for app development. From the testing perspective, it is also known to be highly popular for its creative testing features. In Laravel, there are two ways to test the application. One is with Unit testing and Feature testing. Unit testing allows you to test your classes models etc, while Feature testing allows you to test your code base.

If you look inside your Laravel installation folder, you can see that there are two test subfolders available. These two subdirectories in hosting for the Laravel project are Unit testing and Feature testing. These are the areas where you will be conducting your app's test, either be Unit testing or Feature testing.

How to create a Laravel unit test case?

When you use PHPUnit, the first step is to create the latest test classes. These test classes are stored in the./tests/ folder of your Laravel app. Each test class is named Test.php inside a directory. As this format helps PHPUnit to find each test class simply, and further avoid any other class that doesn’t reside in the Test.php file. In your latest Laravel app, you will find two files in the ./tests/ directory, one is ExampleTest.php and the other is TestCase.php. TestCase.php file is commonly a bootstrap file that sets the Laravel ecosystem and variety of features within our tests. It makes it very simple to use Laravel features in tests, and also allows a framework for testing assistance. The ExampleTest.php merges an example code test class which contains a basic test case using app testing helpers.

For creating a front test class, you can either create a new file manually or can run the artisan make:test command .

Laravel will build a basic test class that looks like this:

<?PHP
class BasicTest extends TestCase{
 /*** A basic test example
** @return void
*/ public function testExample(){
$this->assertTrue(true);  }
}?>

Though this folder includes a lot of information regarding tests, the most important note, for now, is the testsuite folder definition:

<?xml version="1.0" encoding="UTF-8"?>
<PHPUnit ... >
<testsuites>
<testsuite name="Application Test Demo">
<directory>./tests/</directory>
 </testsuite>
</testsuites>
...</phpunit>

How to write a basic unit test?

After finishing the above-mentioned processes successfully, you can now write the basic unit test case for your Laravel app by directing it to the tests/Feature/ExampleTest.php.

Here, you can write test cases for testing the fundamental specialties of the application.

<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ExampleTest extends TestCase
{
 /**
* A basic test example.
*
* @return void
*/
public function testBasicTest()
 {
$response = $this->get('/');
$response->assertStatus(200);
}
}

You can use seven of the basic PHPUnit assertions to write tests for your Basket class. These assertions are:

  • assertTrue()
  • assertFalse()
  • assertEquals()
  • assertNull()
  • assertContains()
  • assertCount()
  • assertEmpty()

What is given, when, then in testing?

If you want to test particular actions of your code, then you should follow the intrinsic pattern of Given, When and Then. Given – It states the primary ecosystem setup you would like to test. You can use some data, or can even set up a model factory in this step. When – When refers to a specific function and called at some particular stage of the testing process. Then – In this part, you declare what the result should look like.

How to test a Laravel application?

Unit testing is not the only testing process you will need for your app. Though it is most necessary in most cases and must be an important part of your creating process, it is not necessary that it will complete all your testing needs. Sometimes your application has complex displays, navigation, and forms, and you want to test these particulars too.

This is where Laravel’s test assistance comes into play and makes testing of a few particulars simple just as the Unit testing does.

In past Laravel unit testing examples, we saw the automatic files within the /tests/ directory and left the ./tests/ExampleTest.php file to review at the next stage. So open it now, it should look like this:

<?php
class ExampleTest extends TestCase
{
/**
* A basic functional test example
*
* @return void
*/
public function testBasicExample()
{
$this->visit('/')
->see('Laravel 5.5');
}
}

As you can see, the test in this scene is normally simple and very quick to understand. Without having any prior knowledge about how Laravel test assistance works, you can understand that it outputs some like this:

  • when I visit / (webroot)
  • I should see ‘Laravel 5

When you open the app in your web browser, you will see a splash screen with “Laravel 5” written on it. After successfully passing a test with PHPUnit, it is right to say that the output of this example test is spot-on.

This test ensures that the web page made at the / path, gives output with the text ‘Laravel 5’. Though it looks like an easy test and doesn’t seem much critical, whenever your web app needs to display sensitive information, tests like this can simply avert you from deploying broken apps.

If you have any doubts about the above topic or have to get services and consultations and get the Laravel test services. Feel free to contact us. AIRO GLOBAL SOFTWARE will be your strong digital partner. E-mail id: [email protected]

enter image description here

Author - Johnson Augustine
Chief Technical Director and Programmer
Founder: Airo Global Software Inc
LinkedIn Profile: www.linkedin.com/in/johnsontaugustine/

enter image description here

Testing code is one of the most major parts to understand your app and the behavior of code. All the developer knows how hurtful bugs can be, especially in the creating stage as it takes hours of effort. Testing each stage can be as good as believing in yourself. You write and run your code, without knowing how it actually behaves if some exception occurs, this may cause a problem in production. So, executing test cases plays a major role in SDLC. You give a better life and believe your code with your eyes closed, once your examination coverage is at a good level.

Today, we are going to write an easy test method and execute test code using tools like Xdebug and PHPUnit. PHPUnit is a programmer-based testing framework. This is an excellent testing framework for writing Unit tests for PHP Web Apps. With the help of PHPUnit, we can direct test-driven improvement.

How to install PHPUnit?

wget https://phar.phpunit.de/phpunit-7.5.phar
chmod +x phpunit-7.5.phar
sudo mv phpunit-7.5.phar /usr/bin/phpunit

There are several other possible ways to install PHPUnit. Now, To get our Test Coverage full record we need to install Xdebug:

sudo apt install php-pear
pecl install xdebug
sudo apt-get install php-xdebug

Without making any further delay Let's dive into our easy test stages where we test whether an array is empty:

  • Create a file UnitTest where we will include all testable files
  • In this folder, create a subfolder test
  • Create a new file phpunit.xml
  • In this subfolder and add the below code

    /UnitTest/tests/ /UnitTest/tests/

What are the Basic Conventions to Write Unit Test Case?

Below are some basic conventions and steps for writing tests with PHPUnit:

  • Test File names should have a suffix Test.
  • Same as it, If the class name is MyFirstClass then the test class name will be MyFirstClassTest.
  • Add test as the prefix for method names.
  • All testing types are public.
  • MyFirstClassTest class should be inherited

These are the major instructions for the PHP unit testing framework. The basic configurations and settings are all set up. It is now time to write the prime test case.

How to Write The First Unit Test Case in PHP?

Build a file EmptyTest.php in UnitTest/tests. Add the below code to it.

<?PHP
use PHPUnit\Framework\TestCase;
class EmptyTest extends TestCase
{
 public function testFailure()
{
$this->assertEmpty(['something']);
 }
}
?>

Now, to execute our test code inside terminal type:

phpunit i.e. phpunit tests/EmptyTest.php.

By executing only phpunit, it automatically executes all test file inside UnitTest/tests folder because we have mentioned this directory in phpunit.xml above. We will get our test failure because the value in the array is not empty. In the terminal you are getting like this:

F                                                                   1 / 1 (100%)
Time: 513 ms, Memory: 10.00MB
There was 1 failure:
EmptyTest::testFailure
Failed asserting that an array is empty
/home/anu/myproj/testcase/tests/EmptyTest.php:8

FAILURES!

Tests: 1, Assertions: 1, Failures: 1

Let's pass our above test case and add one more step to test equals.

<?PHP
use PHPUnit\Framework\TestCase;
class EmptyTest extends
TestCase
{

public function

testFailure()
{
$this->assertEmpty([]);
 }
}
?>
<?PHP
use PHPUnit\Framework\TestCase;
class EqualsTest extends TestCase
{
public function testFailure()
{
$this->assertEquals(0,1); //failed asserting 0 and 1 are equal
}
}
?>

It gives an unexpected error when $expected is not the same as $actual. If $expected is the same to $actual then it is true. Don’t forget that the first argument is expected and the other is actual. The above test only walks away if the expected (0) is equal to (1). To pass the above test case just replace 0 with 1 or 1 with 0. Now, execute both test cases. Your both tests must pass:

PHPUnit 7.5.6 by Sebastian Bergmann and contributors.
Runtime:       PHP 7.3.1-1+ubuntu18.04.1+deb.sury.org+1 with Xdebug 2.7.0
Configuration: /home/samrat/myproj/UnitTest/phpunit.xml
..                                                                  2 / 2 (100%)
Time: 200 ms, Memory: 10.00MB
OK (2 tests, 2 assertions)

Let’s see another example in which a developer has built a code to parse the items received in JSON and validate it for the possible use case may be an exception or for other validation instruction.

Let's build an app/item_parser.php file inside the UnitTest directory

<?PHP
namespace App\Parser;
require_once 'exceptions.php';
use App\Exceptions\JsonParseException;
use App\Exceptions\InvalidNumberFormatException;
class ItemsParser {
public static function parse($response){
$jDecode=json_decode($response);
if (JSON_ERROR_NONE !== json_last_error()){
throw new JsonParseException('Error parsing
JSON:'.$response);
}
$jd=$jDecode->_embedded->menu_items;
$parsed = [];
foreach($jd as $key => $item){
$price=$item->price_per_unit;
if (is_string($price)) {
throw new 
InvalidNumberFormatException('Invalid numeric value encountered:'.$price);
}
$parsed[$item->id] = [
"label" => $item->name,
"value" => number_format(($price/100), 2, '.', ' ')
];
}
return $parsed;
}
}
?>

create a class called ItemsParser with a static option called parse that takes one argument called a response, received from JSON data. We have checked for different types of exceptions and returned parsed data as an array.

Let's add some exceptions classes for the above use cases inside the same folder:

<?PHP
namespace App\Exceptions;
class JsonParseException extends
\Exception {}
class InvalidNumberFormatException 
extends \Exception {}
?>

Next in the UnitTest/tests folder, create a new file called ItemsParserTest.php and add the below test code:

<?php
use PHPUnit\Framework\TestCase;
require_once __DIR__ . '/../app/item_parser.php';
use App\Parser\ItemsParser;
class ItemsParserTest extends TestCase
{   
public function test_create_ItemsParser() {
 $ip = new ItemsParser();
$this->assertEquals('App\Parser\ItemsParser',
\get_class($ip));
}
}
?>

Run the above test code, Our test case must pass. By, this we can make sure that ItemParser class is successfully instantiated. As we don't invoke real endpoint. Let's create a fake json data called one.json inside UnitTest/json and test.

{
"_embedded": {
"menu_items": [
{
"id": "1",
"in_stock": null,
"name": "Pizza",
"open": false,
"pos_id": "101",
"price_per_unit": 899
}
]
}
}

Now Let's write another test code to ensure our data is actually parsed the way we aimed it:

<?php
use PHPUnit\Framework\TestCase;
require_once __DIR__ . '/../app/item_parser.php';
require_once __DIR__ . '/../app/exceptions.php';
use App\Exceptions\JsonParseException;
use App\Parser\ItemsParser;
use App\Exceptions\InvalidNumberFormatException;
class ItemsParserTest extends TestCase
{
var $dir = __DIR__ . '/../json/';
 public function test_create_ItemsParser() {
$ip = new ItemsParser();
 $this->assertEquals('App\Parser\ItemsParser', \get_class($ip));
}
 public function     test_parseSingleItem_shouldReturnArrayOneLabelAndValue(){
$items=file_get_contents($this->dir.'one.json');
$expected = ['1' => ['label' => 'Pizza', 'value' => '8.99']];
$this->assertSame($expected, ItemsParser::parse($items));
 }
}
?>

As, we make fake data, and send that data to ItemParser::parse() option. If we now execute our test case we get the data the way we expected.

PHPUnit 7.5.8 by Sebastian Bergmann and contributors.
Runtime:       PHP 7.2.15-0ubuntu0.18.04.1 with Xdebug 2.6.0
Configuration: /home/samrat/myproj/UnitTest/phpunit.xml
..                                                                  2 / 2 (100%)
Time: 377 ms, Memory: 10.00 MB
OK (2 tests, 2 assertions)

Basically, what we did is we send json details and decoupled them on our data only with label and value as keys and returned only data that is needed as an array. Next, Let's test for the array keys we are getting:

<?php
use PHPUnit\Framework\TestCase;
require_once __DIR__ . '/../app/item_parser.php';
require_once __DIR__ . '/../app/exceptions.php';
use App\Exceptions\JsonParseException;
use App\Parser\ItemsParser;
use App\Exceptions\InvalidNumberFormatException;
class ItemsParserTest extends TestCase
{
 var $dir = __DIR__ . '/../json/';
 public function 
test_JsonParser_arrayKey_shouldReturnKey_label_and_value(){
$items=file_get_contents($this->dir.'one.json');
 $itemId='1';
$parsed=ItemsParser::parse($items);
$this->assertArrayHasKey('label',$parsed[$itemId]);
 $this->assertArrayHasKey('value',$parsed[$itemId]);
 }
}
?>

From the above test code, we can make sure that whether we are getting the key we expected that is label and value. What about the wishes we included in our methods, whether they are executed the way we wanted? Well, I am not sure so let's test it. Say, we have some JSON data with price in the string:

{
"_embedded": {
"menu_items": [
{
"id": "1",
"in_stock": 
"name": "Pizza",
"open": false,
"pos_id": "101",
"price_per_unit": "iamstring"
},
{
"id": "2",
"in_stock": 
"name": "Burger",
"open": false,
"pos_id": "101",
"price_per_unit":"gfsgd"
}
]
}
}

How does our test code block respond when we send price as string, well we must be sure our test code handles such kinds of exceptions.

public function
test_stringPriceValueProvided_shouldThrow_InvalidNumberFormatException(){
$items=file_get_contents("stringPrice.json")
 $this->expectException(InvalidNumberFormatException::class);
$parsed=ItemsParser::parse($items);
}

expectException() method is to ensure whether an expected exception is thrown by our code block. Run phpunit and check the above code it must by the way!

Rechange, the ItemParser.php file we have used the number_format function to convert cent price into the dollar. So, Let's enter a test to check whether our price has been perfectly converted.

public function testCent_Conversion_toDollar(){
$items=$this->file_get_contents('one.json');
 $itemId='1';
 $parsed= ItemsParser::parse($items);
$this->assertEquals(8.99,$parsed[$itemId]['value']);
 }

And These tests can go on and on, wouldn't it be nice to get a report or analyzer for our test case. To get a total test coverage report would be best. So, we have written some test code and let's generate our test report. It's easy just type this in terminal: phpunit --coverage-html . In my case phpunit --coverage-html report/. You will get whole report in html format. By the way, we can generate in xml, php, text and many other formats. Just type: phpunit --help for more.

The above test coverage explains, our first example test coverage is complete but ItemParserTest could be great and we only covered some part of our code so let's stick with partially completed. I leave it up to you to make it full. Actually, partially test code coverage can be acceptable coverage in the software world.

We also get the report of where we can do better. Here are some other code coverage statistics:

How about the Conclusion?

This article explains a basic setup that helps you in getting started with PHPUnit for PHP unit testing.

Hope you find this blog helpful. Unit Testing is a vast topic. Here I have given you a brief introduction so that you can start entering your own tests.

If you have any questions about the above topic or have to get services and consultations and get the unit test services. Feel free to contact us. AIRO GLOBAL SOFTWARE will be your strong digital partner.

E-mail id: [email protected]

enter image description here

Author - Johnson Augustine
Chief Technical Director and Programmer
Founder: Airo Global Software Inc
LinkedIn Profile: www.linkedin.com/in/johnsontaugustine/

enter image description here

If you want an iOS app do you know the pain of distributing apps to your beta testers? Completing the UDIDs into the portal that mobile app testers can work and then trying them to get your app through the ad-hoc network was actually a bit of pain. But now that’s run to change for good in the new and latest version.

Do you know what all features make perfect shiny iPhones and iPods? Of course, the new software packs a punch of a variety of features that are beneficial both for users as well as developers. The testing app that is used called TestFlight will help the developers to make the IOS app smoother. Just to fulfil that wish of yours, we have decided to bring you a blog that will explain the features of TestFlight. In this blog, we will also guide you through the variety of feature functionality and explain how you can make the best use of the Testflight app.

What Is Known As Testflight?

Testflight is a clear simple app that will help you install the beta versions of apps that are used for testing. But now it is so much simpler than it was earlier with testing for iOS app development and app testers. Apple has been to go the simple way with beta testing for iOS.

How To Test Your Ios App In 3 Simple Steps?

  • First, you have to upload the app to the app store connect.
  • Secondly, you have to send an invite to those who are going to test the app
  • At last download test flight app and start testing

How To Release The App To The App Store?

With the test flight app, people are testing what will be the version of the app.you will need to go until the end of the submission process to download the .ipa file to be uploaded to App Store Connect. Use the transporter that is now replaced by the application loader to upload this file to your App Store Connect account, just as you would do if you were to put it on the App Store for users.

How do you send an invite to those who are going to test the app?

Once your .ipa file has been uploaded to the App Store, you can invite different users to test your app. In the industry there are two types of testers you can invite

  • The first one is internal tester: These people are your App Store Connect users. Unless these people are from your team or the people who have been given access to your App Store Connect account, there are some chances that you're ready to invite internal testers to test one of your projects which means your app. If you do, Be aware that you can invite-only 25 internal testers for each project.
  • The second one is external tester: You don’t need to have an App Store Connect account to be an external tester for the app. You can invite any people that you want to test your app, up to 10,000 external testers for each project.

When you invite external testers for testing your app, Apple will check your app and give the approval to allow external testing.

This is the reason why you'll have to provide some information to the Apple review team about your project that means your app they review about:

  • What's the purpose of the app that you made?
  • What do you want people to test your app?
  • who Apple contact during the verification process if needed?

Enter the email of the testers that are testing externally for the verification and add the build to the test. As soon as you have been approved for beta testing, you will receive an email from Apple to inform you. For testers to be ready to start testing your app, go back to App Store Connect and click the Send Invites button under Prerelease for your app.

How to Download Test Flight app and start testing?

The testers that you are invited to will receive an email with a link to test your app. To be ready to test, At first, they will need to download the TestFlight app. After accepting the invitation that you send, they will be going to the TestFlight app and they will be directed to install the app to be tested. The app will appear on the phone's springboard, and also in the TestFlight app. When started, a test session lasts up to 90 days. After that period it's no longer able to test the app unless you upload another build and you start a new round.

What are the advantages of test flight?

  • No need to make a record in App Store Connect to jump into the testing phase
  • Not needed any validation to have anyone testing your app
  • No need to have the UDID of the testers build a test version

What are the disadvantages of test flight?

  • You need to build a record in App Store Connect, but, at the end of the day, you will definitely build one anyway to publish your app.
  • Validation is needed to have external testers to test the app that you made.
  • No clue about how long it takes to review the app
  • Each testing is time-limited If you have any questions about the above topic or have to get mobile app testing services and consultations about and other digital solutions. Feel free to contact us. AIRO GLOBAL SOFTWARE will be your strong digital partner. E-mail id: [email protected]

enter image description here

Author - Johnson Augustine
Chief Technical Director and Programmer
Founder: Airo Global Software Inc
LinkedIn Profile: www.linkedin.com/in/johnsontaugustine/