12. Why testing?
• 10+ classes!
• 2k+ lines!
• ~200 tests (UT & functional)!
• ran in < 3sec in browser!
• ran in < 1sec in CLI!
!
!
http://parsleyjs.org/doc/tests.html
13. Why testing?
max: function (value) {
return $.extend(new Validator.Assert().LessThanOrEqual(value), { priority: 30 });
UT : validate your methods API and behavior
!
}
it('should have a max validator', function () {
expect(parsleyValidator.validate('foo', parsleyValidator.max(10))).to.be(false);
expect(parsleyValidator.validate('5', parsleyValidator.max(10))).to.be(true);
expect(parsleyValidator.validate('10', parsleyValidator.max(10))).to.be(true);
expect(parsleyValidator.validate('42', parsleyValidator.max(10))).to.be(false);
});
✓ should have a max validator
Code
Test
Result
14. Why testing?
max: function (value) {
return $.extend(new Validator.Assert().LessThan(value), { priority: 30 });
Prevent regressions, ensure 3rd party libs consistency
!
}
it('should have a max validator', function () {
expect(parsleyValidator.validate('foo', parsleyValidator.max(10))).to.be(false);
expect(parsleyValidator.validate('5', parsleyValidator.max(10))).to.be(true);
expect(parsleyValidator.validate('10', parsleyValidator.max(10))).to.be(true);
expect(parsleyValidator.validate('42', parsleyValidator.max(10))).to.be(false);
});
1) should have a max validator
Code
Test
Result
17. Why testing?
Fixes bugs found to ensure they’ll never show up again
!
it('should have a max validator', function () {
expect(parsleyValidator.validate('foo', parsleyValidator.max(10))).to.be(false);
expect(parsleyValidator.validate('5', parsleyValidator.max(10))).to.be(true);
expect(parsleyValidator.validate('10', parsleyValidator.max(10))).to.be(true);
expect(parsleyValidator.validate('42', parsleyValidator.max(10))).to.be(false);
expect(parsleyValidator.validate('5', parsleyValidator.max(”10”))).to.be(true);
});
✓ should have a max validator
Code
Test
Result
max: function (value) {
return $.extend(new Validator.Assert().LessThan(value), {
priority: 30,
requirementsTransformer: function () {
return 'string' === typeof value && !isNaN(value) ? parseInt(value, 10) : value;
}
});
}
18. Why testing?
it('should show custom error message with variabilized parameters', function () {
$('body').append('<input type="text" id="element" value="bar" data-parsley-minlength="7" data-parsley-minlength-
message="foo %s bar"/>');
var parsleyField = $('#element').psly();
parsleyField.validate();
!
expect($('ul#parsley-id-' + parsleyField.__id__ + ' li').text()).to.be('foo 7 bar');
});
Functional test : validate your end-user behavior
21. What need to be tested?
• Application logic (services, algorithms, microapps)!
• API responses!
• Application behavior, End-user responses!
!
TO
BE
TESTED
31. How to test?
class SynchronizedUserTest extends PHPUnit_Framework_TestCase
{
/**
* @covers ::construct()
* @expectedException InvalidArgumentException
* @expectedExceptionMessage User and IntercomUser are not the same.
*/
public function testConstructWithWrongEmails()
{
$intercomUser = new IntercomUser(1, 'bar@foo.fr');
$user = (new User)->setEmail('foo@bar.fr');
$this->setProperty($user, 'id', 1);
!
new SynchronizedUser($intercomUser, $user);
}
!
/**
* @covers ::construct()
* @expectedException InvalidArgumentException
* @expectedExceptionMessage User and IntercomUser are not the same.
*/
public function testConstructWithWrongIds()
{
$intercomUser = new IntercomUser(2, 'foo@bar.fr');
$user = (new User)->setEmail('foo@bar.fr');
$this->setProperty($user, 'id', 1);
!
new SynchronizedUser($intercomUser, $user);
}
}
32. How to test?
class SynchronizedUser
{
private $intercomUser;
private $user; !
/**
* @throws InvalidArgumentException If the user intercom and user wisembly doesn't match
*/
public function __construct(IntercomUser $intercomUser, User $user)
{
if ($intercomUser->getUserId() !== $user->getId() || $intercomUser->getEmail() !== mb_strtolower($user->getEmail(),
mb_detect_encoding($user->getEmail()))) {
throw new InvalidArgumentException('User and IntercomUser are not the same.');
} !
$this->intercomUser = $intercomUser;
$this->user = $user;
} !
/**
* @return IntercomUser
*/
public function getIntercomUser()
{
return $this->intercomUser;
} !
/**
* @return User
*/
public function getUser()
{
return $this->user;
}
}
36. Limits
• CSS glitches!
• User experience!
• Browser compatibility!
• Do not take to much time to code / maintain tests!!
• Do not have too long test suites!!
• Fixtures for functional tests!
• Isolate tests among themselves!!
!
37. Going further
• Script & test deploy scripts..!
• SLA & performance tests..!
!