Wrestling with Python Unit testing. Warren Viant



Similar documents
Intro to scientific programming (with Python) Pietro Berkes, Brandeis University

Exemplar Work for SAMs. Units A452 and A453

CIS 192: Lecture 13 Scientific Computing and Unit Testing

Survey of Unit-Testing Frameworks. by John Szakmeister and Tim Woods

Stat 20: Intro to Probability and Statistics

GCE. Computing. Mark Scheme for January Advanced Subsidiary GCE Unit F452: Programming Techniques and Logical Methods

GCE. Economics. Mark Scheme for June Advanced GCE F581 Markets in Action. Oxford Cambridge and RSA Examinations

New Tools for Testing Web Applications with Python

Software Testing. Theory and Practicalities

Functional Skills English Writing Level 1

Advanced Topics: Biopython

CME 193: Introduction to Scientific Python Lecture 8: Unit testing, more modules, wrap up

Fail early, fail often, succeed sooner!

How Inbound Marketing With HubSpot Changed Our Business

ESKITP5023 Software Development Level 3 Role

EDME 532: TESOL INTERNSHIP AND SEMINAR SUMMER / 2015

2010 Software Development GA 3: Written examination

Computer Science Practical Programming

Creating an Interactive Digital Animation Level: 3 Sub-level: Unit 312 Credit value: 8 Guided learning hours: 65

English Language Arts Test Book 2

Summary of assessment and checklist for the new GCE Health and Social Care Unit HSC08 (optional)

Python Testing with unittest, nose, pytest

Exemplar Candidate Work

Physical Assessment & Clinical Judgment Rubric

GCE. Business Studies. Mark Scheme for January Advanced GCE Unit F296: Business Production. Oxford Cambridge and RSA Examinations

That s Not Fair! ASSESSMENT #HSMA20. Benchmark Grades: 9-12

1 Why should monitoring and measuring be used when trying to improve services?

here: styleguide.googlecode.com/svn/trunk/javascriptguide.xml.

Self-review 9.3 What is PyUnit? PyUnit is the unit testing framework that comes as standard issue with the Python system.

Accredited Specimen Mark Scheme

The structured application of advanced logging techniques for SystemVerilog testbench debug and analysis. By Bindesh Patel and Amanda Hsiao.

Source Code Management for Continuous Integration and Deployment. Version 1.0 DO NOT DISTRIBUTE

The Agile Movement An introduction to agile software development

Unit Testing webmethods Integrations using JUnit Practicing TDD for EAI projects

Integrated Skills in English (ISE) Guide for Students ISE I (B1) Reading & Writing Speaking & Listening

Unit testing with mock code EuroPython 2004 Stefan Schwarzer p.1/25

MASTER OF SCIENCE IN COMPUTER SCIENCE

SECURITY MANAGEMENT Produce security risk assessments

How to Create a Chapter Mailing Step-by-step guide

Mark Scheme. Business Studies BUSS4. (Specification 2130) Unit 4: The Business Environment and Change

DSS Data Exchange Task Card 1 How to access the DSS Data Exchange

Randomized algorithms

CSTE Mock Test - Part I - Questions Along with Answers

An Introduction to text-based test automation and the TextTest tool

Test Driven Development in Assembler a little story about growing software from nothing

NATIONAL CERTIFICATES (VOCATIONAL)

Iteration CHAPTER 6. Topic Summary

SpiraTest / SpiraTeam Automated Unit Testing Integration & User Guide Inflectra Corporation

The importance of supporting children and young people s communication skills and what The Communication Trust does to enable this Shona Crichton,

Combining structured data with machine learning to improve clinical text de-identification

Computer-based Language Learning with Interactive Web Exercises

Revised (Version 2) January For teaching from 2012 For awards from 2014 COMPUTER SCIENCE SPECIMEN ASSESSMENT MATERIALS. WJEC CBAC Ltd.

KPIs and Scorecards using OBIEE 11g Mark Rittman, Rittman Mead Consulting Collaborate 11, Orlando, Florida, April 2011

How To Write A Task For Ielts

DIGITAL COMPOSITION SPECIALIST II DEPARTMENTAL PROMOTIONAL EXAMINATION

HCC ONLINE COURSE REVIEW RUBRIC

2. Roles and responsibilities

HISTORY KEY STAGE THREE

Mathematical goals. Starting points. Materials required. Time needed

Unit Testing & JUnit

City of De Pere. Halogen How To Guide

klm Mark Scheme Applied Business 8611/8613 General Certificate of Education Financial Planning and Monitoring 2009 examination - January series

DALLAS COUNTY NON-EXEMPT PERSONNEL PERFORMANCE EVALUATION

1 Hour, Closed Notes, Browser open to Java API docs is OK

Kids College Computer Game Programming Exploring Small Basic and Procedural Programming

Question 1 Formatted: Formatted: Formatted: Formatted:

Deliver, monitor and evaluate customer service to external customers OCR unit number 329 Sector unit number F/601/2551

Format OCR ICR. ID Protect From Vanguard Systems, Inc.

HENLEY BUSINESS SCHOOL DOCTORAL PROGRAMME SPECIFICATION DOCTOR OF BUSINESS ADMINISTRATION

Mark Scheme. Business Studies BUSS1. (Specification 2130) Unit 1: Planning and Financing a Business

Kanban game. Danske Bank version developed by Sune Lomholt based on Software development Kanban Christina Skaskiw

Final. Mark Scheme ICT INFO3. (Specification 2520) Unit 3: The Use of ICT in the Digital World. General Certificate of Education (A-level) June 2012

Enable Your Automated Web App Testing by WebDriver. Yugang Fan Intel

February 11, Megabyte Money.notebook. Megabyte Money. Electronic "money" that replaces currency & checks. Sep 18-8:51 PM

CS 2112 Spring Instructions. Assignment 3 Data Structures and Web Filtering. 0.1 Grading. 0.2 Partners. 0.3 Restrictions

GCE. Religious Studies. Mark Scheme for January Advanced Subsidiary GCE Unit G572: Religious Ethics. Oxford Cambridge and RSA Examinations

Model Assignment Issued September 2011 OCR Administration (Business Professional) UNIT 26 (LEVEL 4) - ANALYSE FINANCIAL INFORMATION

Digital Literacy: Communicating, Collaborating and E- Safety, Multimedia, Music and Sound, Digital Imagery

Math Board Games. For School or Home Education. by Teresa Evans. Copyright 2005 Teresa Evans. All rights reserved.

Unit 21: Hosting and managing websites (LEVEL 3)

MEng, BSc Applied Computer Science

Python and Google App Engine

Arrk purple paper. ios App store submission guide. India. India

GCE Religious Studies. Mark Scheme for June Unit G581: Philosophy of Religion. Advanced GCE. Oxford Cambridge and RSA Examinations

The ITIL v.3. Foundation Examination

Appendix 10: Improving the customer experience

Transcription:

Wrestling with Python Unit testing Warren Viant

Assessment criteria OCR - 2015 Programming Techniques (12 marks) There is an attempt to solve all of the tasks using most of the techniques listed. The techniques are used appropriately in all cases giving an efficient, working solution for all parts of the problem. Design (9 marks) There is a detailed analysis of what is required for these tasks justifying their approach to the solution. There will be a full set of detailed algorithms representing a solution to each part of the problem. There is detailed discussion of testing and success criteria. The variables and structures are identified together with any validation required. Development (9 marks) There is detailed evidence showing development of the solution with evidence of systematic testing during development to show that all parts work as required. The code is well organised with meaningful variable names and detailed annotation indicating the function of each section. Testing (9 marks) The test plan covers all major success criteria for the original problem with evidence to show how each of these criteria have been met, or if they have not been met, how the issue might be resolved. There is a full evaluation of the final solution against the success criteria. A high level of written communication will be obvious throughout the task and specialist terms/technology with accurate use of spelling will have been used. Grammar and punctuation are used correctly and information is presented in a coherent and structured format.

How to test your code? Traditional approach Plan a series of tests on paper Manually conduct the tests If there is a failure, fix the code and repeat the tests Do your pupils enjoy testing?

Test driven development (TDD) Before writing any Python code you first write an automated test. While writing the test you can focus on the correct input and output for your code. The advantage is that at this stage you are not concerned with how you implement your code. Run your test and your test should fail. This is ok, as you have yet to implement your algorithm. Begin programming your algorithm. Run your test again, if it fails it means you have more work to do. Once the code passes the test, move onto the next algorithm.

Planning your test Think about what the function should do What inputs it will need What outputs are expected

Example Python unit test Python provides an automated testing facility that is linked to PyCharm 1. import unittest 2. from Task_1 import * 3. class TestSequenceFunctions(unittest.TestCase): 4. def test_rolldie(self): 5. maxnumofsides = 20 6. for die in range(2, maxnumofsides+1): 7. value = rolldie(die) 8. self.asserttrue(value >= 1) 9. self.asserttrue(value <= maxnumofsides)

Example Python unit test Python provides an automated testing facility that is linked to PyCharm 1. import unittest 2. from Task_1 import * 3. class TestSequenceFunctions(unittest.TestCase): 4. def test_rolldie(self): 5. maxnumofsides = 20 6. for die in range(2, maxnumofsides+1): 7. value = rolldie(die) 8. self.asserttrue(value >= 1) 9. self.asserttrue(value <= maxnumofsides)

Example Python unit test Python provides an automated testing facility that is linked to PyCharm 1. import unittest 2. from Task_1 import * 3. class TestSequenceFunctions(unittest.TestCase): 4. def test_rolldie(self): 5. maxnumofsides = 20 6. for die in range(2, maxnumofsides+1): 7. value = rolldie(die) 8. self.asserttrue(value >= 1) 9. self.asserttrue(value <= maxnumofsides)

Example Python unit test Python provides an automated testing facility that is linked to PyCharm 1. import unittest 2. from Task_1 import * 3. class TestSequenceFunctions(unittest.TestCase): 4. def test_rolldie(self): 5. maxnumofsides = 20 6. for die in range(2, maxnumofsides+1): 7. value = rolldie(die) 8. self.asserttrue(value >= 1) 9. self.asserttrue(value <= maxnumofsides)

Example Python unit test Python provides an automated testing facility that is linked to PyCharm 1. import unittest 2. from Task_1 import * 3. class TestSequenceFunctions(unittest.TestCase): 4. def test_rolldie(self): 5. maxnumofsides = 20 6. for die in range(2, maxnumofsides+1): 7. value = rolldie(die) 8. self.asserttrue(value >= 1) 9. self.asserttrue(value <= maxnumofsides)

Steps to create a unit test 1. Create a new.py file for your test code 2. Import the Python unittest library import unittest 3. Import your code to test from <file name> import * 4. Create a class for your tests by inheriting from unittest.testcase class <class name> (unittest.testcase): 5. Add one or more new test methods def <method name>(self): 6. Add assert statements to your method to notify the system of an error self.assertequal(<variable 1>, <variable 2>) self.asserttrue(<condition> ) 7. Run the test

A sample of the assert methods available in Python s unittest If an assert method returns false, an error is logged and displayed in PyCharm Method assertequal(a, b) assertnotequal(a, b) asserttrue(x) assertfalse(x) assertin(a, b) assertnotin(a, b) Checks that a == b a!= b bool(x) is True bool(x) is False a in b a not in b Documentation https://docs.python.org/3/library/unittest.html

Task 1 Simulating a dice (OCR 2014) A game uses dice with 4, 6 and 12 sides to determine various outcomes. Design, code and test a program that will simulate throwing dice with these numbers of sides. The user should be able to input which dice is being thrown, eg 4, 6 or 12. The program should output the dice chosen and the score e.g. 6 sided dice thrown, score 4 The user should be able to repeat this process as many times as required.

Process 1. Break down the problem into sub-problems 2. Consider writing a function for each sub-problem What name best describes the function? What task does the function perform? What data is input? What data is output? 3. Consider the test case for each function What input data will break the function? What is the correct output for each valid input? 4. Implement the functions and their unit tests