KEMBAR78
Magento2.3 API Functional Testing | PPTX
© 2019 Magento, Inc. Page | 1
API Functional
Testing in Magento 2.3
© 2019 Magento, Inc.
.
Page | 2
Senior Software Developer at BORN Group
Vishwas Bhatnagar
https://www.linkedin.com/in/vishwasbhatnagar/
© 2019 Magento, Inc. Page | 3
Agenda
© 2019 Magento, Inc. Page | 4
Agenda
• Testing In Magento 2
• What is API Functional Testing ?
• Why Do we need API Functional Testing
• API Functional test case writing
• Configuring , running and cleanup after test cases
© 2019 Magento, Inc. Page | 5
Not In Scope
© 2019 Magento, Inc. Page | 6
Not in Scope
• Unit Tests
• Integrations Tests
• Functional Tests
• Magento Testing Framework using Selenium
© 2019 Magento, Inc. Page | 7
Testing In Magento 2
© 2019 Magento, Inc. Page | 8
Tests available in Magento 2
 Unit Tests
 Integration Tests
 Functional Tests
 Performance Tests
 Static Code Analysis Tests
 JavaScript Tests
 Integrity Tests
 Legacy Tests
© 2019 Magento, Inc. Page | 9
Unit Testing
 Individual classes or functions in isolation
 Resides in dev/tests/unit
© 2019 Magento, Inc. Page | 10
Integration Tests
• Interactions between
components, layers, and the
environment
• dev/tests/integration
© 2019 Magento, Inc. Page | 11
Functional Tests
 Same way user would interact
 dev/tests/functional and dev/tests/api-functional
Functional
Test
API
Functional
MTF
© 2019 Magento, Inc. Page | 12
Performance Tests
 Track and maintain performance level per build
 dev/tests/performance
© 2019 Magento, Inc. Page | 13
Few more tests
 Static Code Analysis Tests :
 Static code analysis checks that PHP code
follows the Magento 2 coding standards and
best practices(dev/tests/static)
 JavaScript Tests :
 Tests the accuracy of JavaScript functions.
 (dev/tests/js).
 Integrity testing
 check how an application is linked.
 Basically, they tell us if your application
should be able to run.
 Legacy Tests
© 2019 Magento, Inc. Page | 14
What is API Functional
Testing ?
© 2019 Magento, Inc. Page | 15
API Functional Tests
• Process used within software
development in which software
is tested to ensure that it
conforms with all
requirements.
• Makes sure that nothing visible
to the customer broke.
• Don't care about how
something is built
• Don't care How something
works at a high level
© 2019 Magento, Inc. Page | 16
Why Do we need API
Functional Testing
© 2019 Magento, Inc. Page | 17
API Functional Testing Advantages
• Adheres with Agile software
Development
• Testing parallel with
development
• Finds problems early in the
development cycle
• Allows easy the programmer to
refactor code or upgrade
• Writing the test first forces you
to think through your design
and what it must accomplish
before you write the code
© 2019 Magento, Inc. Page | 18
Getting Started
© 2019 Magento, Inc.
.
Page | 19
Writing Test cases
• Should inherit from the generic test case
MagentoTestFrameworkTestCaseWebapiAbstract.
• Should Use ObjectManager for testing in isolation
• Class /File name should end with test
• Methods should start with test
• All Methods should be declared public
© 2019 Magento, Inc.
.
Page | 20
Sample Test cases
© 2019 Magento, Inc.
.
Page | 21
Assertions
You can make various assertions that
are available in
phpunitFrameworkAssert like
• assertArrayHasKey
• assertCount
• assertSame
• assertEquals
• assertNotEmpty
• assertGreaterThan
• assertIsReadable
• assertNotNull
© 2019 Magento, Inc.
.
Page | 22
Assertion Example
© 2019 Magento, Inc. Page | 23
Running Test Cases
© 2019 Magento, Inc.
.
Page | 24
Configuring magento to run tests
• Copy /dev/tests/api-functional/phpunit_rest.xml.dist to
/dev/tests/api-functional/phpunit_rest.xml
© 2019 Magento, Inc.
.
Page | 25
Configuring magento to run tests
• Copy /dev/tests/api-functional/config/install-config-
mysql.php.dist to /dev/tests/api-functional/config/install-config-
mysql.php.
• Configure your DB connection and install
© 2019 Magento, Inc.
.
Page | 26
Running Using CLI
• Switch to M2_ROOT/dev/test/api-fuctional/
• To Run Test class
– ../../../vendor/bin/phpunit --debug
../../../app/code/Born/VehicleSearch/Test/Api/JobPostTest
• To run a test method
– ../../../vendor/bin/phpunit --debug --filter testJobSaveNoAction
../../../app/code/Born/Jobs/Test/Api/JobPostTest
© 2019 Magento, Inc.
.
Page | 27
Running Using CLI
© 2019 Magento, Inc.
.
Page | 28
Analyzing the Test Result
© 2019 Magento, Inc. Page | 29
Complex Test case Writing ?
© 2019 Magento, Inc. Page | 30
Use DocBlock Annotations
@magentoAppIsolation :
• Enable and Disable
• Application state after a test
run will be the same as before
the test run
@magentoDbIsolation :
• Enable and Disable
• All data, required for a test,
live during transaction only.
Any test results won’t be
written in a database.
© 2019 Magento, Inc. Page | 31
@magentoDataFixture/magentoApiDataFixture:
• Used to setup environment
• All data, required for a test, live during transaction only. Any test results
won’t be written in a database
@magentoAppArea :
• Adminhtml | frontend | global
• Set area to application
© 2019 Magento, Inc. Page | 32
@magentoCache:
• <type>|all enabled|disabled
• Disables cache
@magentoConfigFixture :
• [<store_code>_store] <config_path> <config_value>
• Configuration values for individual tests and revert them after the test
execution
© 2019 Magento, Inc.
.
Page | 33
Fixture Example
© 2019 Magento, Inc. Page | 34
Clean it Up !
© 2019 Magento, Inc. Page | 35
Cleaning data after test case execution
• RollBack Scripts
– If you use fixtures to insert
data you can create a rollback
script for it .
– Same format as the
corresponding fixture: a script
or a method
– Should be Stored in the same
directory
– Same class as the
corresponding fixture and
suffixed with Rollback
© 2019 Magento, Inc.
.
Page | 36
Cleaning data after test case execution
Script
Fixture : Magento/Catalog/_files/categories.php
RollBack : Magento/Catalog/_files/categories_rollback.php
Method
Fixture : MagentoCatalogModelProductTest::prepareProduct
RollBack : MagentoCatalogModelProductTest::prepareProductRollback
© 2019 Magento, Inc.
.
Page | 37
Cleaning data after test case execution
• TearDown Method
– If you want to perform any actions after constraints
– To be included in test case file
© 2019 Magento, Inc. Page | 38
Questions ?
© 2019 Magento, Inc. Page | 39
Thank You

Magento2.3 API Functional Testing

  • 1.
    © 2019 Magento,Inc. Page | 1 API Functional Testing in Magento 2.3
  • 2.
    © 2019 Magento,Inc. . Page | 2 Senior Software Developer at BORN Group Vishwas Bhatnagar https://www.linkedin.com/in/vishwasbhatnagar/
  • 3.
    © 2019 Magento,Inc. Page | 3 Agenda
  • 4.
    © 2019 Magento,Inc. Page | 4 Agenda • Testing In Magento 2 • What is API Functional Testing ? • Why Do we need API Functional Testing • API Functional test case writing • Configuring , running and cleanup after test cases
  • 5.
    © 2019 Magento,Inc. Page | 5 Not In Scope
  • 6.
    © 2019 Magento,Inc. Page | 6 Not in Scope • Unit Tests • Integrations Tests • Functional Tests • Magento Testing Framework using Selenium
  • 7.
    © 2019 Magento,Inc. Page | 7 Testing In Magento 2
  • 8.
    © 2019 Magento,Inc. Page | 8 Tests available in Magento 2  Unit Tests  Integration Tests  Functional Tests  Performance Tests  Static Code Analysis Tests  JavaScript Tests  Integrity Tests  Legacy Tests
  • 9.
    © 2019 Magento,Inc. Page | 9 Unit Testing  Individual classes or functions in isolation  Resides in dev/tests/unit
  • 10.
    © 2019 Magento,Inc. Page | 10 Integration Tests • Interactions between components, layers, and the environment • dev/tests/integration
  • 11.
    © 2019 Magento,Inc. Page | 11 Functional Tests  Same way user would interact  dev/tests/functional and dev/tests/api-functional Functional Test API Functional MTF
  • 12.
    © 2019 Magento,Inc. Page | 12 Performance Tests  Track and maintain performance level per build  dev/tests/performance
  • 13.
    © 2019 Magento,Inc. Page | 13 Few more tests  Static Code Analysis Tests :  Static code analysis checks that PHP code follows the Magento 2 coding standards and best practices(dev/tests/static)  JavaScript Tests :  Tests the accuracy of JavaScript functions.  (dev/tests/js).  Integrity testing  check how an application is linked.  Basically, they tell us if your application should be able to run.  Legacy Tests
  • 14.
    © 2019 Magento,Inc. Page | 14 What is API Functional Testing ?
  • 15.
    © 2019 Magento,Inc. Page | 15 API Functional Tests • Process used within software development in which software is tested to ensure that it conforms with all requirements. • Makes sure that nothing visible to the customer broke. • Don't care about how something is built • Don't care How something works at a high level
  • 16.
    © 2019 Magento,Inc. Page | 16 Why Do we need API Functional Testing
  • 17.
    © 2019 Magento,Inc. Page | 17 API Functional Testing Advantages • Adheres with Agile software Development • Testing parallel with development • Finds problems early in the development cycle • Allows easy the programmer to refactor code or upgrade • Writing the test first forces you to think through your design and what it must accomplish before you write the code
  • 18.
    © 2019 Magento,Inc. Page | 18 Getting Started
  • 19.
    © 2019 Magento,Inc. . Page | 19 Writing Test cases • Should inherit from the generic test case MagentoTestFrameworkTestCaseWebapiAbstract. • Should Use ObjectManager for testing in isolation • Class /File name should end with test • Methods should start with test • All Methods should be declared public
  • 20.
    © 2019 Magento,Inc. . Page | 20 Sample Test cases
  • 21.
    © 2019 Magento,Inc. . Page | 21 Assertions You can make various assertions that are available in phpunitFrameworkAssert like • assertArrayHasKey • assertCount • assertSame • assertEquals • assertNotEmpty • assertGreaterThan • assertIsReadable • assertNotNull
  • 22.
    © 2019 Magento,Inc. . Page | 22 Assertion Example
  • 23.
    © 2019 Magento,Inc. Page | 23 Running Test Cases
  • 24.
    © 2019 Magento,Inc. . Page | 24 Configuring magento to run tests • Copy /dev/tests/api-functional/phpunit_rest.xml.dist to /dev/tests/api-functional/phpunit_rest.xml
  • 25.
    © 2019 Magento,Inc. . Page | 25 Configuring magento to run tests • Copy /dev/tests/api-functional/config/install-config- mysql.php.dist to /dev/tests/api-functional/config/install-config- mysql.php. • Configure your DB connection and install
  • 26.
    © 2019 Magento,Inc. . Page | 26 Running Using CLI • Switch to M2_ROOT/dev/test/api-fuctional/ • To Run Test class – ../../../vendor/bin/phpunit --debug ../../../app/code/Born/VehicleSearch/Test/Api/JobPostTest • To run a test method – ../../../vendor/bin/phpunit --debug --filter testJobSaveNoAction ../../../app/code/Born/Jobs/Test/Api/JobPostTest
  • 27.
    © 2019 Magento,Inc. . Page | 27 Running Using CLI
  • 28.
    © 2019 Magento,Inc. . Page | 28 Analyzing the Test Result
  • 29.
    © 2019 Magento,Inc. Page | 29 Complex Test case Writing ?
  • 30.
    © 2019 Magento,Inc. Page | 30 Use DocBlock Annotations @magentoAppIsolation : • Enable and Disable • Application state after a test run will be the same as before the test run @magentoDbIsolation : • Enable and Disable • All data, required for a test, live during transaction only. Any test results won’t be written in a database.
  • 31.
    © 2019 Magento,Inc. Page | 31 @magentoDataFixture/magentoApiDataFixture: • Used to setup environment • All data, required for a test, live during transaction only. Any test results won’t be written in a database @magentoAppArea : • Adminhtml | frontend | global • Set area to application
  • 32.
    © 2019 Magento,Inc. Page | 32 @magentoCache: • <type>|all enabled|disabled • Disables cache @magentoConfigFixture : • [<store_code>_store] <config_path> <config_value> • Configuration values for individual tests and revert them after the test execution
  • 33.
    © 2019 Magento,Inc. . Page | 33 Fixture Example
  • 34.
    © 2019 Magento,Inc. Page | 34 Clean it Up !
  • 35.
    © 2019 Magento,Inc. Page | 35 Cleaning data after test case execution • RollBack Scripts – If you use fixtures to insert data you can create a rollback script for it . – Same format as the corresponding fixture: a script or a method – Should be Stored in the same directory – Same class as the corresponding fixture and suffixed with Rollback
  • 36.
    © 2019 Magento,Inc. . Page | 36 Cleaning data after test case execution Script Fixture : Magento/Catalog/_files/categories.php RollBack : Magento/Catalog/_files/categories_rollback.php Method Fixture : MagentoCatalogModelProductTest::prepareProduct RollBack : MagentoCatalogModelProductTest::prepareProductRollback
  • 37.
    © 2019 Magento,Inc. . Page | 37 Cleaning data after test case execution • TearDown Method – If you want to perform any actions after constraints – To be included in test case file
  • 38.
    © 2019 Magento,Inc. Page | 38 Questions ?
  • 39.
    © 2019 Magento,Inc. Page | 39 Thank You