4. No one writes a PERFECT CODE :)
Software Testing?
It’s impossible to guarantee a bug-free release, but you
can minimize the chances by using automated tests.
There’s nothing better than ending your day with a
clean conscience knowing you did your job right.
5. Test cases are executed manually without any support
from tools or scripts.
Manual Testing
6. Test cases are executed with the assistance of tools,
scripts, and software.
Automated Testing
7. Manual/Automated Testing: Pros and Cons
Manual Automated
Not accurate. More reliable
Time Consuming Process Faster than a manual approach.
It does not involve in programming
task to fetch hidden information.
Testers can test complicated
application too.
Useful in UI Testing Not always!
8. Automated software testing can increase the depth and
scope of tests to help improve software quality.
Why do we need automation testing?
Lengthy tests that are often avoided during manual
testing can be run unattended.
9. A unit test is a piece of code that exercises another
piece of code.
What are Unit Tests?
A unit test should check that one piece of code, such as
a function or method, runs properly and returns the
expected results.
10. How to setup unit tests for your code.
We will walk through
How to write the unit tests.
How to run the tests.
How to write a testable code.
13. A good unit test for our hm_get_vat() function would try
passing in various numbers in various formats to
determine if the results are always what we expect them
to be.
14. A unit test is nothing more than a programmatic check
to determine if a chunk of code performs the way it is
supposed to.
17. Easiest way to setup Unit Tests is through WP-CLI
WP-CLI, a command line interface for WordPress.
http://wp-cli.org/
https://make.wordpress.org/cli/handbook/installing/
19. 1. Go to your WordPress install root folder
cd /Applications/MAMP/htdocs/phpunit/
2. Instruct WP-CLI to create the initial unit test files
wp scaffold plugin-tests <plugin-slug>
This will generate all of the files needed for our unit
tests.
20. wp scaffold child-theme Generate child theme based on an existing theme.
wp scaffold plugin Generate starter code for a plugin.
wp scaffold plugin-tests Generate files needed for running PHPUnit tests in a plugin.
wp scaffold post-type Generate PHP code for registering a custom post type.
wp scaffold taxonomy Generate PHP code for registering a custom taxonomy.
wp scaffold theme-tests Generate files needed for running PHPUnit tests in a theme.
wp scaffold _s Generate starter code for a theme based on _s.
https://developer.wordpress.org/cli/commands/scaffold/
21. The new folders / files created:
bin/
install-wp-tests.sh
tests/
bootstrap.php
test-sample.php
phpunit.xml
.travis.yml
These files are the foundation of our plugin’s
test suite.
23. The testing suite that WP-CLI sets up for us includes
one sample unit test
It is located in tests/test-sample.php:
class SampleTest extends WP_UnitTestCase {
function testSample() {
// replace this with some actual testing code
$this->assertTrue( true );
}
}
26. This tells PHPUnit where to look for the PHP file
tests/bootstrap.php defines a few options, and then
also tells PHPUnit where the actual unit tests live
Here files which are prefixed with “test-“ are only
considered
Note: only methods prefixed with “test” will be
considered a unit test. All other methods will be
skipped.
27. Things to note down while writing tests
Separate the tests into multiple files so that they are
logically organized
Every class contains an extension of the
WP_UnitTestCase class.
The name of the class does not really matter, just name
it something that makes sense.
28. Assertions
An assertion is one “check” within the the test to
determine if a value is equal to what we expect.
Tests
Group of one or more assertions are Tests
29. One test with a single assertion:
function test_ktm_message() {
$string = ‘WordCamp Kathmandu 2017';
$this->assertEquals( 'WordCamp Kathmandu 2017',
$string );
}
One test with a two assertions:
function test_ktm_message() {
$string = ‘WordCamp Kathmandu 2017 - Awesome Event‘;
$this->assertEquals( 'WordCamp Kathmandu 2017 - Awesome
Event', $string );
$this->assertNotEquals( 'WordCamp Kathmandu 2017',
$string );
}
https://phpunit.de/manual/current/en/appendixes.assertions.html