Presentation is loading. Please wait.

Presentation is loading. Please wait.

Powerpoint Templates Page 1 Powerpoint Templates Unit Testing Ari Seppi

Similar presentations


Presentation on theme: "Powerpoint Templates Page 1 Powerpoint Templates Unit Testing Ari Seppi"— Presentation transcript:

1 Powerpoint Templates Page 1 Powerpoint Templates Unit Testing Ari Seppi (ari.seppi@uta.fi)

2 Powerpoint Templates Page 2 Agenda -Basic introductory to unit testing -What is unit testing? -Why to unit test? -When to unit test? -How to unit test? -Practical examples -PHP and databases -Even more practical exercise

3 Powerpoint Templates Page 3 So? What Is It? -White box testing -The test developer has knowledge about implementation details. -Usually the same developer writes the functionality and unit tests to test it. -Testing of a single unit of an application. -A class is a natural scope in object oriented languages and a module in procedural languages.

4 Powerpoint Templates Page 4 What Can Be Tested With It? -Since scope of a test was single class, the tested functionality is also limited to it. -Data structures -Execution paths -Invalid parameters -One problem is figuring out when the testing is good enough. -Naturally it is often not even possible to test function with all the possible parameter values. -Dynamically typed languages bring their own problems to the mix as there is no compiler level parameter type checking.

5 Powerpoint Templates Page 5 Why? -Low level unit tests are easy to automate as opposed to higher level user interface tests. -Unit test force developers to think the code through one more time and so reveal error prone parts of the code at the early stage. -Fixing early saves money and work hours and not fixing something might cost even more.

6 Powerpoint Templates Page 6 When? -Test driven development -Unit testing suits especially well for writing the tests before the actual so that the code can be tested as soon as its ready. -But writing the tests immediately after the implementation works as well. -Making the tests in some undefined future is a pretty sure way of not doing them at all.

7 Powerpoint Templates Page 7 How? -Many languages already have unit testing frameworks. -JUnit, PHPUnit etc. -The unit tests are very easily automated so they are often run in project’s continous integration system. -Unit testing can be used to test most of the API level issues like the interfaces and data structures -What if the methods are used in a wrong way? -How are error situations handled?

8 Powerpoint Templates Page 8 How? – Object Oriented Languages -Object oriented languages: In practice tests are arranged into test case classes based on the classes on the system under test. -A simple single tests covers a single method/function more complicated test cases can cover state changed by calling functions consecutively.

9 Powerpoint Templates Page 9 How? – But units have relations -One of the problems in test unit creation lies in the external relations to other units and functionality outside our own code all together. -Libraries -Data sources such as databases -To solve the problem we need to emulate the outside world. -Stubs, mocks and fakes -Test beds (includes hardware) and harnesses (testing framework)

10 Powerpoint Templates Page 10 How? – Code Coverage -Code coverage tells us how much of the code is involved in the test cases. -If a function has 50 different paths of execution and one of them gets tested, the tests cannot say much about the actual quality of the code. -Of course one path of execution might be the one taken by 99% of the real life executions. -Testing all the possible executions is often more than the project schedule allows for. -And even if you test all the paths of execution, unexpected function parameters may create new error situations even on the tested paths.

11 Powerpoint Templates Page 11 Summary of the Basics -Unit tests are created by developers, usually by the developer who wrote the tested code, and executed by any developer who modifies the code. -Or in practice often in continous integration systems. -We should unit test since it saves time in the future. -Often it is impossible to test all the possible cases so it is important to see which cases need to be tested.

12 Powerpoint Templates Page 12 Let’s Get Practical -https://svn.sis.uta.fi/jenkins-testihttps://svn.sis.uta.fi/jenkins-testi -Username: redmine -Password: 15c3d75f8019f18af08aeaf24f5ba37e -http://jenkins.sis.uta.fi/http://jenkins.sis.uta.fi/ -Username: TestProject -Password: testunitproject -http://jenkins.sis.uta.fi/sonar/dashboard/ind ex/robotwarhttp://jenkins.sis.uta.fi/sonar/dashboard/ind ex/robotwar

13 Powerpoint Templates Page 13 Testing Databases -Unit tests should be relatively fast to execute but exporting large test data sets for to databases can take time. -One solution could be using in-memory databases but with them we have the problem that they do not function exactly like the actual production database. -One solution could be performing some standard model tests in in-memory database.

14 Powerpoint Templates Page 14 Exercise -Study file https://svn.sis.uta.fi/jenkins- testi/PHPTest/robotwar/model/RobotUtils.ph phttps://svn.sis.uta.fi/jenkins- testi/PHPTest/robotwar/model/RobotUtils.ph p -Create a unit test file for it in file RobotUtils[student_number]Test.php to the appropriate directory.

15 Powerpoint Templates Page 15 Exercise 2 -Study file https://svn.sis.uta.fi/jenkins- testi/PHPTest/robotwar/model/RobotManager.phphttps://svn.sis.uta.fi/jenkins- testi/PHPTest/robotwar/model/RobotManager.php -Create a unit test file for function saveRobots in file RobotManager[student_number]Test.p hp to the appropriate directory.

16 Powerpoint Templates Page 16 References and More Reading -Testing course at TUT (in Finnish) http://www.cs.tut.fi/~testaus/s2013/luennot/TIE- 21200_2013.pdf http://www.cs.tut.fi/~testaus/s2013/luennot/TIE- 21200_2013.pdf -Testing with PHPUnit https://jtreminio.com/2013/03/unit-testing-tutorial- introduction-to-phpunit/ https://jtreminio.com/2013/03/unit-testing-tutorial- introduction-to-phpunit/ https://jtreminio.com/2013/03/unit-testing-tutorial- introduction-to-phpunit/ https://jtreminio.com/2013/03/unit-testing-tutorial- introduction-to-phpunit/ -Testing PHP/MySQL Applications with PHPUnit/DBUnit http://en.oreilly.com/mysql2008/public/asset/attach ment/1980%E2%80%8E http://en.oreilly.com/mysql2008/public/asset/attach ment/1980%E2%80%8E


Download ppt "Powerpoint Templates Page 1 Powerpoint Templates Unit Testing Ari Seppi"

Similar presentations


Ads by Google