unit testing

13

Click here to load reader

Upload: aleksey-tyulpin

Post on 29-Jun-2015

181 views

Category:

Software


3 download

DESCRIPTION

Author shows what is testing, gives information about unit testing, nose framework and TDD. Most of information author learned from course "Software Quality and Testing" at the University of Oulu, Finland in 2013

TRANSCRIPT

Page 1: Unit testing

Aleksey Tyulpin, NArFU, [email protected]

Unit testingBasics, Test Driven Development

Page 2: Unit testing

Plan● Basics of Unit testing● Nose framework● Exercise● Test driven development

Page 3: Unit testing

What is testing?● Testing is the process of executing a program with the intent of finding

errors. (Mayers 1979)

● Software testing is a formal process carried out by a specialized testing team in which a software unit, several integrated software units or an entire software package are examined by running the programs on a computer. All the associated tests are performed according to approved test procedures on approved test cases.

(Galin 2004)

Page 4: Unit testing

What is testing?● Testing is the process of executing a program with the intent of finding

errors. (Mayers 1979)

● Software testing is a formal process carried out by a specialized testing team in which a software unit, several integrated software units or an entire software package are examined by running the programs on a computer. All the associated tests are performed according to approved test procedures on approved test cases.

(Galin 2004)

Page 5: Unit testing

What is testing?● Program testing can be used to show the presence of

bugs, but never their absence(Dijkstra 1972)

● Central question of software testing is “what is a test criterion?”

(Goodenough and Gerhart [1975, 1977])

Page 6: Unit testing

Software testing objectives● Direct

○ To identify and reveal as many errors as possible in the tested software.

○ To bring the tested software, after correction of the identified errors and retesting, to an acceptable level of quality.

○ To perform the required tests efficiently and effectively, within budgetary and scheduling limitations.

● Inderect○ To compile a record of software errors for use in error prevention (by

corrective and preventive actions).(Galin 2004)

Page 7: Unit testing

Unit Testing● Unit testing (UT) is a method for modular testing of a

program’s functional behavior● Program is decomposed into units - collection sof

functions● Units are independently tested● Such testing requires specification of values for the

inputs (or test inputs) to the unit(Koushik Sen, Darko Marinov, Gul Agha 2005)

Page 8: Unit testing

A.A.A. Phases● Arrange - prepare test. Step includes preparation of

input data and expected result● Act - call what you test● Assert - compare the result of called method (or

function) to the expected result

Page 9: Unit testing

Nose framework● Simple possible failing test:

def test():assert False

● Simple passing test:def test():

pass

● Nose collects tests automatically from python source files, directories and packages found in its working directory

● For running tests you have to use tool nosetests:nosetests3 [options] [(optional) test files or directories]

● I prefer to use such command:nosetests3 -v

Page 10: Unit testing

How to write tests ● Every test should be simple and perform on one set of input data● Name of every test should explain what test checks● Remember about A.A.A. phases ● Name test modules/files starting with ‘test_’● Name test functions starting with ‘test_’● Example:

def test_MultiplyShouldRetun12For3And4():a, b = 3,4 # Arrangeres = multiply(a,b) # Actassert res == 12 # Assert

Page 11: Unit testing

Palindrome checker ● In your home directory there is a folder - Dev/nose_palindrome● In this floder there is a module which contains function isPalidrome(word),

where variable word is a string.○ Example

>>> import palindrome as p>>> p.isPalindrome('kazak')True

● Task - write tests and find as many bugs as possible. All possible kinds of tests are allowed.

Page 12: Unit testing

Test Driven Development● Kent Beck, 2003● Steps:

○ Add fail-test○ Write code○ Run test○ Refactor the code

● Writing only the code necessary to pass tests

● Writing the tests first drives a deeper and earlier understanding of the product requirements