文件名称:Django 1.1 Testing and Debugging.pdf
文件大小:7.25MB
文件格式:PDF
更新时间:2022-09-03 07:06:24
Django Testing Debugging
Table of Contents Preface 1 Chapter 1: Django Testing Overview 9 Getting started: Creating a new application 10 Understanding the sample unit test 10 Understanding the sample doctest 11 Running the sample tests 12 Breaking things on purpose 15 Test errors versus test failures 20 Command line options for running tests 23 Verbosity 24 Settings 27 Pythonpath 27 Traceback 27 Noinput 27 Version 28 Summary 28 Chapter 2: Does This Code Work? Doctests in Depth 29 The Survey application models 29 Testing the Survey model 31 Testing Survey model creation 31 Is that test useful? 34 Developing a custom Survey save method 35 Deciding what to test 39 Some pros and cons of doctests so far 40 Additional doctest caveats 44 Beware of environmental dependence 44 Beware of database dependence 47 Table of Contents [ ii ] Beware of test interdependence 52 Beware of Unicode 57 Summary 62 Chapter 3: Testing 1, 2, 3: Basic Unit Testing 63 Unit tests for the Survey save override method 63 Pros of the unit test version 65 Cons of the unit test version 69 Revisiting the doctest caveats 69 Environmental dependence 70 Database dependence 70 Test interdependence 75 Unicode 75 Providing data for unit tests 76 Providing data in test fixtures 76 Example test that needs test data 76 Using the admin application to create test data 78 Writing the function itself 83 Writing a test that uses the test data 85 Extracting the test data from the database 86 Getting the test data loaded during the test run 87 Creating data during test set up 92 Summary 96 Chapter 4: Getting Fancier: Django Unit Test Extensions 97 Organizing tests 98 Creating the survey application home page 101 Defining the survey application URLs 102 Developing views to serve pages 104 Creating templates for pages 106 Testing the survey home page 110 Creating the survey detail pages 117 Refining the survey detail view 117 Templates for the survey detail pages 118 Basic testing of the survey detail pages 119 Customizing the admin add and change survey pages 122 Developing a custom survey form 122 Configuring admin to use the custom form 123 Testing the admin customization 124 Additional test support 130 Supporting additional HTTP methods 130 Maintaining persistent state 130 E-mail services 130 Table of Contents [ iii ] Providing test-specific URL configuration 131 Response context and template information 131 Testing transactional behavior 132 Chapter 5: Filling in the Blanks: Integrating Django and Other Test Tools 135 Problems of integration 136 Specifying an alternative test runner 138 Creating a new management command 141 How much of the code are we testing? 144 Using coverage standalone 145 Integrating coverage into a Django project 149 The twill web browsing and testing tool 153 Using the twill command line program 155 Using twill in a TestCase 159 Summary 167 Chapter 6: Django Debugging Overview 169 Django debug settings 170 The DEBUG and TEMPLATE_DEBUG settings 170 The TEMPLATE_STRING_IF_INVALID setting 171 Debug error pages 172 Database query history 176 Debug support in the development server 179 Handling problems in production 182 Creating general error pages 183 Reporting production error information 183 Internal server error notifications 184 Page not found notifications 185 Summary 187 Chapter 7: When the Wheels Fall Off: Understanding a Django Debug Page 189 Starting the Survey voting implementation 190 Creating test data for voting 191 Defining a question form for voting 192 Debug page #1: TypeError at / 195 Elements of the debug page 196 Basic error information 197 Traceback 198 Request information 203 GET 203 POST 203 FILES 203 Table of Contents [ iv ] COOKIES 203 META 204 Settings 204 Understanding and fixing the TypeError 205 Handling multiple Survey questions 209 Creating the data for multiple questions 209 Coding support for multiple questions 210 Debug page #2: TemplateSyntaxError at /1/ 211 Understanding and fixing the TemplateSyntaxError 212 Recording Survey responses 213 Coding support for recording Survey responses 214 Debug page #3: NoReverseMatch at /1/ 218 Understanding and fixing the NoReverseMatch exception 219 Debug page #4: TemplateDoesNotExist at /thanks/1/ 221 Understanding and fixing TemplateDoesNotExist 223 Handling invalid Survey submissions 224 Coding custom error message and placement 226 Debug page #5: Another TemplateSyntaxError 229 Fixing the second TemplateSyntaxError 232 Summary 233 Chapter 8: When Problems Hide: Getting More Information 235 Tracking SQL queries for a request 236 Settings for accessing query history in templates 236 SQL queries for the home page 237 Packaging the template query display for reuse 243 Testing the repackaged template code 247 SQL queries for the active Survey form display page 252 SQL queries for posting survey answers 255 The Django Debug Toolbar 256 Installing the Django Debug Toolbar 257 Debug toolbar appearance 258 The SQL panel 260 The Time panel 263 The Settings panel 264 The HTTP Headers panel 264 The Request Vars panel 265 The Templates panel 266 The Signals panel 267 The Logging panel 268 Redirect handling by the debug toolbar 268 Table of Contents [ v ] Tracking internal code state 273 Resist the urge to sprinkle prints 273 Simple logging configuring for development 275 Deciding what to log 277 Decorators to log function entry and exit 277 Applying the decorators to the Survey code 280 Logging in the debug toolbar 282 Summary 284 Chapter 9: When You Don't Even Know What to Log: Using Debuggers 285 Implementing the Survey results display 285 Results display using pygooglechart 288 Getting started with the debugger 293 The list command 295 The where command 296 The args command 300 The whatis command 300 The print and pp commands 300 Debugging the pygooglechart results display 301 The step and next commands 301 The continue command 307 The jump command 307 The break command 309 The clear command 310 Fixing the pygooglechart results display 312 The up and down commands 312 The return command 316 Results display using matplotlib 319 Improving the matplotlib approach 324 Setting up static file serving 325 Dynamically generating image files 327 Dealing with race conditions 329 Using the debugger to force race situations 334 Notes on using graphical debuggers 344 Summary 345 Chapter 10: When All Else Fails: Getting Outside Help 347 Tracking down a problem in Django 348 Revisiting the Chapter 7 voting form 348 Is the right code actually running? 350 Is the code correct as per the documentation? 351 Table of Contents [ vi ] Searching for a matching problem report 354 Another way to search for a matching problem report 359 Determining the release that contains a fix 363 What if a fix hasn't been released yet? 365 What if a fix hasn't been committed yet? 366 What if a ticket has been closed without a fix? 367 Tracking down unreported problems 368 Where to ask questions 369 Tips on asking questions that will get good answers 370 Opening a new ticket to report a problem 372 Summary 374 Chapter 11: When it's Time to Go Live: Moving to Production 375 Developing an Apache/mod_wsgi configuration 376 Creating the WSGI script for the marketr project 378 Creating an Apache VirtualHost for the marketr project 379 Activating the new Apache configuration 380 Debugging the new Apache configuration 383 Configuring Apache to serve static files 391 Testing multithreaded behavior 396 Generating load with siege 397 Load testing the results recording code 399 Fixing the results recording code 404 Additional load testing notes 406 Using Apache/mod_wsgi during development 407 Summary 409 Index 411