Lecture 3: Programs and Testing CS-A1123 Basics in Programming Y2 Winter and Spring 2020 Timo Kiravuo, .Sc. Objects and User Interfaces Data Structures in Objects

• Programs manipulate data and they need to store it somewhere • During processing: data structures in memory • In general: the file system (exercise 4) • A database is a specific service that uses the file system • Data structures need to be designed • This will likely become apparent with the exercises • The Object Oriented model is not just an implementation feature • It is a key technology for modeling and designing software Testing

• After implementation testing is mandatory • First by thinking things through • Walk through your design • Will be a bit challenging along this course, as we are still learning the basics, too • Once there is written code, various tools can be used • Testing can be a programming methodology, too • Test Driven Development (TDD) • Figure out what the software has to do and write tests first • Then write the program to pass the tests PyQt User Interface Library

• PyQt is a software library for providing graphical features • A large feature set and fast execution • Works on different platforms (Windows, , MacOS) • Contains about 600 classes and 6000 functions and methods • Implements the graphics toolkit for Python • Others graphics libraries exist for Python, too • PySide, PyGTK, wxPython, • Matches graphical features on the screen (widgets) to objects in software Using PyQT the Procedural Way

• Procedural programming (from Y1) uses subprograms • Subprograms typically operate on data structures in the program's memory • This creates a window on the display using PyQt5 import sys from PyQt5.QtWidgets import QWidget, QPushButton, QApplication def Example(): app = QApplication(sys.argv) w = QWidget() b = QPushButton('Push', w) b.clicked.connect(w.close) w.show() sys.exit(app.exec_()) Doing the Same in Object Oriented Way

• We create class that contains both data and functionality • This brief sample does not show how to use the class class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.__btn = QPushButton('Push', self) self.show() def activate_close(self): self.__btn.clicked.connect(self.close) self.show() Observations on the Comparison

• We could make the procedural version more general by using arguments • Functionality is still defined by this one subprogram • Object oriented version could use more methods • But out object is already very capable • Type "help(Example)" to see this • Functionalities are bound to the object and come from parent classes • Other paradigms exist • Functional, rule based logic etc. • In general it is recommended to use one paradigm for one software project Errors and Testing Errors, Mistakes and Finding Them

• Errors in software are often obvious • Once found, locating them can be very hard • Main problem is in specifying the actual task to be done • A perfect specification is easily as long as the software implementation • Most specifications are informal (natural language) • An implemented program can be considered to be the specification • Validating • Are we doing the right thing? • Is our specification right? • Verification • Is our implementation right, does it correspond to the specification? Static and Dynamic Testing and Analysis

• Static methods look at the code without any particular input • Interpreters and compilers do this as part of their process • Produce warnings and errors • Defining data types is often useful for static analysis • Various tools are available for common languages for code analysis • Dynamic methods execute the software with input and check output • Repeatable tests with defined data • Random tests to catch errors not even expected by testers • Both methods are commonly used for testing as part of the production process Types of Testing

• Typically there are different levels of testing during a project • Unit testing: verify the functionality of a particular function or class • Integration testing: combine multiple components and test their interoperation • Systems testing: testing the overall system, can include performance testing • Acceptance testing: Verifying that the product meets the specification • Regression testing: repeating already passed tests after changes to the software • Types of tests and their coverage depend on the project • Automation of testing is very important Testing as Part of Programming

• Testing can be a separate process and also performed by the themselves • Generally people are not very good at testing their own code, as they focus on its expected behavior • Testing is a separate discipline of software development and large projects usually have specific testing staff • Finding and fixing the actual mistake in code is left to the programmers Coverage of Tests

• Coverage, how much of the created program is tested, is a central issue • Many ways to measure coverage • Function coverage: have all functions been called? • Branch coverage: have all branches been taken? • Clause coverage: has each condition been evaluated as both true and untrue • Path coverage: has every possible path the software can take been executed • Usually not possible to reach • Even complete coverage does not guarantee that there are no mistaks • Formally proving a program correct is not feasible in practice, due to the near infinite amount of branches a program's execution can take Unit Testing on this Course

• A typical unit test consists of • Test code that calls the unit, usually brief • Written description of the test, e.g. which methods to call in order, such as: Add(0), Add(1), Delete(0) • Test input to be used • Possibly the program state before the test (which variables to be set etc.) • Expected output and program state after the test • Unit tests usually use a library of framework to take care of housekeeping matters • Runs the tests, records output, reports results • There is more to testing, this is sufficient for this course Basic Testing Features of Python Module unittest

• Object oriented testing methodology • Designed to test classes • Module contains several classes • TestCase for defining individual tests • TestSuite to create the whole test package • TestRunner to perform the tests • Other useful tools are • unittest.mock to emulate functionality that does not exist • Module doctest picks test cases embedded in documentation in the module file An Unittest Sample import unittest class TestStringMethods(unittest.TestCase): def test_upper(self): self.assertEqual('foo'.upper(), 'FOO') def test_isupper(self): self.assertTrue('FOO'.isupper()) self.assertFalse('Foo'.isupper()) def test_split(self): s = 'hello world' self.assertEqual(s.split(),['hello','world']) # check s.split fails when not a string with self.assertRaises(TypeError): s.split(2) Notes on module unittest

• Pay attention to setting up tests • Variables and other data structures • Unit tests may be executed in any order • Module unittest alphabetical by default • If your tests depend on the execution order, it is not unit testing any more... A Bit More about Computers and Programming Networked, distributed and embedded computing • Most computing devices are now networked • Internet the most common example • Networking creates distributed systems • The actual network is hidden by the overall system • Internet itself consists of millions of subnets that are not visible to its users • Embedded systems are computers in devices that do not look like computers • Car's brake systems, household appliances etc. • IoT, Internet of Things Terminal Devices, Servers and the Cloud

• Different roles for different computing devices in the network • Based on use and location • E.g. video servers and mobile phones • Client-server model is currently a very common architecture • Client software in the terminal device • App in mobile phone, JavaScript in browser • Server in the cloud • The cloud is flexible networked computing resource, where data can be stored and software rapidly deployed as needed • This means that code will be executed in different environments • Hard to predict where your program will end up • Impacts on performance, security etc. Software Stacks and Packages

• Systems consists usually of several software components • These are connected by interfaces • Inside the code these are usually just names • Note the Python import command • PyQt is actually written in ++, but this does not matter • Externally the whole is referred to as software stack • Software stack: all components surrounding the code being developed • Python's own software stack has many packages • The standard library implements only part of the full functionality • The number of various packages and their versions is very large • Package and version management is an important part of software development Virtual Environments

• Common operating systems usually contain an installed software stack of their own • Browser, various libraries • Can easily create interference with the packages needed by particular software, such as our own Python development • Python supports virtual environments • venv and virtualenv • Limit the scope of a particular process and control which packages are available • Python also contains tools for package management • pip installer • pip can also install packages to the virtual environments • When using your computer for development virtual environments help avoiding clutter • It is easy to install too many packages with overlapping names Recommendation

• Figure out how to use the Python "venv" on your own computer before continuing this course • Later exercises require installing some packages with "pip" and you might not want them on your computer after the course • Easy as, using subdirectory foo: python3 -m venv foo . foo/bin/activate pip3 some nice packages ... do stuff ... deactivate Exercises Exercise 3: Tested Code

• Learn to test your code • Things are getting harder now, this will take more time • Read the assignment first and think, before starting • Two main tasks • Testing the function "weekdays" • Unit testing • Linked list • Meet the specification