Ratnadeep Debnath Testing your Django App.

Slides:



Advertisements
Similar presentations
Chapter 6 Server-side Programming: Java Servlets
Advertisements

PHP Form and File Handling
WEB DESIGN TABLES, PAGE LAYOUT AND FORMS. Page Layout Page Layout is an important part of web design Why do you think your page layout is important?
Test Automation: Coded UI Test
Presenter: James Huang Date: Sept. 29,  HTTP and WWW  Bottle Web Framework  Request Routing  Sending Static Files  Handling HTML  HTTP Errors.
JSP1 Java Server Pages (JSP) Introducing JavaServer Pages TM (JSP TM ) JSP scripting elements.
Composition One object is composed of other objects (e.g. Car is composed of an engine, etc.) Engine cylinders : int volume: int start() : None Car engine.
Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department.
Unit Testing Using PyUnit Monther Suboh Yazan Hamam Saddam Al-Mahasneh Miran Ahmad
Unittest in five minutes Ray Toal How to unit test Not manually, that's for sure You write code that exercises your code Perform assertions.
Automation using Selenium Authored & Presented by : Chinmay Sathe & Amit Prabhu Cybage Software Pvt. Ltd.
Overview of JSP Technology. The need of JSP With servlets, it is easy to – Read form data – Read HTTP request headers – Set HTTP status codes and response.
ACM Web Development Workshop - PHP By Luis Torres.
DIY Web Development Hand Code Your Own Page (For Free!) by Bryan Brown, Indiana University Bloomington SLIS.
Introduction to Silverlight. Slide 2 What is Silverlight? It’s part of a Microsoft Web platform called Rich Internet Applications (RIA) There is a service.
Selenium automated testing in Openbravo ERP Quality Assurance Webinar April 8th, 2010.
Sustainable SharePoint 2010 Customizations By Bill Keys.
1 Accelerated Web Development Course JavaScript and Client side programming Day 2 Rich Roth On The Net
A centre of expertise in digital information managementwww.ukoln.ac.uk Accessibility Testing Brian Kelly UKOLN University of Bath Bath, BA2 7AY
@DNNCon Don’t forget to include #DNNCon in your tweets! Effective Unit Testing for DNN James McKee Solutions Developer / Enterprise
Top Five Web Application Vulnerabilities Vebjørn Moen Selmersenteret/NoWires.org Norsk Kryptoseminar Trondheim
Chapter 6 Server-side Programming: Java Servlets
FireBug. What is Firebug?  Firebug is a powerful tool that allows you to edit HTML, CSS and view the coding behind any website: CSS, HTML, DOM and JavaScript.
Django 101 By: Jason Sumner. Django Overview Django was started in 2003, released under BSD in 2005, and the Django Software Foundation was established.
Introduction to JavaScript CS101 Introduction to Computing.
RUBRIC IP1 Ruben Botero Web Design III. The different approaches to accessing data in a database through client-side scripting languages. – On the client.
Server-side Programming The combination of –HTML –JavaScript –DOM is sometimes referred to as Dynamic HTML (DHTML) Web pages that include scripting are.
Matthew Grove Portal Developers Workshop, February 2008 Portal integration and code reuse in portlets.
 Previous lessons have focused on client-side scripts  Programs embedded in the page’s HTML code  Can also execute scripts on the server  Server-side.
Unit, Regression, and Behavioral Testing Based On: Unit Testing with JUnit and CUnit by Beth Kirby Dec 13, 2002 Jules.
Introduction to JavaScript MIS 3502, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 2/2/2016.
Wes Preston DEV 202. Audience: Info Workers, Dev A deeper dive into use-cases where client-side rendering (CSR) and SharePoint’s JS Link property can.
JavaScript Invented 1995 Steve, Tony & Sharon. A Scripting Language (A scripting language is a lightweight programming language that supports the writing.
CSC 108H: Introduction to Computer Programming
Dive into web development
COSC405 Web Application Engineering II Slides adopted from here
CX Introduction to Web Programming
Topic 2: Hardware and Software
Class03 Introduction to Web Development (Hierarchy and the IDE)
Leverage your Business with Selenium Automation Testing
Introduction to gathering and analyzing data via APIs Gus Cavanaugh
Business Directory REST API
Getting Started with Plot.ly
ASP MVP Web applications and Razor
Data Virtualization Tutorial… CORS and CIS
Test Driven Development 1 November Agenda  What is TDD ?  Steps to start  Refactoring  TDD terminology  Benefits  JUnit  Mocktio  Continuous.
Style Generally style is built into Python, as it demands indents. However, good Python style is set out in PEP8:
CMPE419 Mobile Application Development
Intro to PHP & Variables
Introduction to Silverlight
SharePoint Cloud hosted Apps
SharePoint-Hosted Apps and JavaScript
React Revived Web Driver IO for Testers
Magento 2 Development For more information visit:
WEB 407 Competitive Success/snaptutorial.com
WEB 407 Education for Service-- snaptutorial.com.
WEB 407 Teaching Effectively-- snaptutorial.com
Test Automation For Web-Based Applications
Introduction to JavaScript
Unit 27 - Web Server Scripting
The Application Lifecycle
A second look at JavaScript
Lightning Component Testing with Jasmine Jasmine is a behaviour-driven development framework - that is used for the purpose of testing Javascript code.
Agile testing for web API with Postman
Introduction to JavaScript
Architecture of the web
An Introduction to JavaScript
Cross-Site Scripting Issues and Defenses Ed Skoudis Predictive Systems
CMPE419 Mobile Application Development
Lab 1: D3 Setup John Fallon
Presentation transcript:

Ratnadeep Debnath Testing your Django App

Why test? ● Check if your app behaves as it is supposed to ● Refactoring code does not break existing app's behaviour

Django test frameworks ● Doctests ● Unit tests

Doctests def my_func(a_list, idx): """ >>> a = ['larry', 'curly', 'moe'] >>> my_func(a, 0) 'larry' >>> my_func(a, 1) 'curly' """ return a_list[idx] Tests that are embedded in your functions' docstrings and are written in a way that emulates a session of the Python interactive interpreter

Unit tests Unit testing is a method by which individual units of source code are tested to determine if they are fit for use import unittest class MyFuncTestCase(unittest.TestCase): def testBasic(self): a = ['larry', 'curly', 'moe'] self.assertEqual(my_func(a, 0), 'larry') self.assertEqual(my_func(a, 1), 'curly')

Benefits of Unit tests ● Facilitates change ● Simplifies Integration ● Documentation ● Design

Writing unit tests ● Django's unit tests use a Python standard library module: unittest. This module defines tests in class-based approach. from django.utils import unittest ● Django test runner looks for unit tests in two places: ● models.py ● tests.py, in the same directory as models.py ● The test runner looks for any subclass of unittest.TestCase in this module.

Writing unit tests from django.test import TestCase from django.test.client import Client from posts.models import Post class TestPosts(TestCase): def setUp(self): self.post = Post.objects.create(title='foo', body='foo') def test_views(self): self.assertEqual(self.post.title, 'foo') self.assertEqual(self.post.body, 'foo')

Running tests ● $./manage.py test ● $./manage.py test posts ● $./manage.py test posts.TestPosts ● $./manage.py test posts.TestPosts.test_views

The Test Client ● A python class that acts as a dummy web browser ● Allows testing your views ● Interact with your Django app programatically

Let's run tests

The Test Client ● A way to build tests of the full stack ● Acts more or less like a browser ● Stateful ● Default instance on django.test.TestCase ● Can be instantiated outside test framework

The test client isn't ● A live browser test framework ● Selenium ● Twill ● Windmill

Why is it different? ● TestClient can't do some things ● No JavaScript ● DOM validation or control ● Can do some things that browsers can't

Returns a response ● response.status_code ● response.content ● response.cookies ● response['Content-Disposition']

... and a little more ● response.template ● The template(s) used in rendering ● response.context ● The context objects used in rendering ● response.context['foo']

...and it maintains state ● self.cookies ● self.session

Login/Logout from django.test.client import Client c = Client() c.login(username='foo', Password='password') c.logout()

Get a page from django.test.client import Client c = Client() c.get('/foo/') c.get('/foo/?page=bar') c.get('/foo/', {'page': 'bar'})

Post a page from django.test.client import Client c = Client() c.post('/foo/') c.post('/foo/?bar=3') c.post('/foo/', data={'bar': 3}) c.get('/foo/?whiz=4', data={'bar':3})

Rest of HTTP c.put('/foo/') c.options('/foo/') c.head('/foo/') c.delete('/foo/')

A common problem r = self.client.get('/foo') self.assertEquals(r.status_code, 200) FAIL Assertion Error: 301 != 200

The fix r = self.client.get('/foo', follow=True) self.assertEquals(r.status_code, 200) response.redirect_chain -- links visited before a non-redirect was found

You can do more ● Extra Headers c = TestClient(HTTP_HOST='foo.com') c.get('/foo/', HTTP_HOST='foo.com') ● Files f = open('text.txt') c.post('/foo/', content_type=MULTIPART_CONTENT), data = {'file': f}) f.close()

Assertions ● assertContains() ● assertNotContains() ● assertFormError() ● assertTemplateUser() ● assertTemplateNotUsed() ● assertRedirects()

Do's and Don'ts ● Don't rely on assertContains ● Assertions on template content are weak ● Test at the source ● Is the context right? ● Are the forms correct? ● Django's templates make this possible!

When to use TestClient ● When you need to test the full stack ● Interaction of view and middleware ● Great for testing idempotency

Let's run some tests again

Coverage ● Why do I need coverage tests? ● An small example: ● You write some new code :-) ● Does it really work as expected? :-( ● Now, you write tests for it :-) ● Does your test really test your code? :-( ● This is where coverage comes to the rescue :D

What is coverage ● A tool for measuring code coverage of Python programs. ● ● It monitors your program, noting which parts of the code have been executed, ● It then analyzes the source to identify code that could have been executed but was not. ● Coverage measurement is typically used to gauge the effectiveness of tests. It can show which parts of your code are being exercised by tests, and which are not.

Who is behind Coverage Ned Batchelder

Install Coverage ● easy_install coverage ● pip install coverage

How to use Coverage? ● Use coverage to run your program and gather data ● $ coverage -x my_program.py arg1 arg2 ● Use coverage to report on the results $ coverage -rm Name Stmts Miss Cover Missing my_program % 33-35, 39 my_other_module % TOTAL %

How to use Coverage ● Generate better reports for presentation, say html: ● coverage html -d ● Erase existing coverage data ● coverage -e

Let's use Coverage

Questions?

Useful links ● ●

Contact Ratnadeep Debnath IRC: rtnpro Blog: ratnadeepdebnath.wordpress.comratnadeepdebnath.wordpress.com

Thank You :)