| 390 | | - **Part 2: les doctests** |smile| |
| 391 | | - `Part 3: le DDD` |
| 392 | | |
| 393 | | |
| 394 | | XXXXXXXXX |
| 395 | | |
| 396 | | The plan |
| 397 | | ======== |
| 398 | | |
| 399 | | - `A few definitions` |
| 400 | | - `Part 1: doing TDD in Python` |
| 401 | | - **Part 2: writing documents in reST** |
| 402 | | - `Part 3: writing doctests` |
| 403 | | - `Part 4: doing DDD in Python` |
| 404 | | |
| 405 | | Part 2 |
| 406 | | ====== |
| 407 | | |
| 408 | | Writing documents in reST |
| 409 | | |
| 410 | | - **What is reST ?** |
| 411 | | - `reST syntax overview` |
| 412 | | - `reST tools` |
| 413 | | |
| 414 | | What is reST ? |
| 415 | | ============== |
| 416 | | |
| 417 | | reSTructuredText (say reSt to be hype) is: |
| 418 | | |
| 419 | | - a plaintext markup syntax and parser system |
| 420 | | - easy to read as-is, yet powerful |
| 421 | | - the Pythonistas laTeX |
| 422 | | - see http://docutils.sf.net |
| 423 | | |
| 424 | | What is reST ? |
| 425 | | ============== |
| 426 | | |
| 427 | | Example:: |
| 428 | | |
| 429 | | ================== |
| 430 | | I am the reST file |
| 431 | | ================== |
| 432 | | |
| 433 | | I am the first section |
| 434 | | ====================== |
| 435 | | |
| 436 | | I am the content of the first section. |
| 437 | | I have things to say. |
| 438 | | |
| 439 | | I am the second section |
| 440 | | ======================= |
| 441 | | |
| 442 | | I am, what I am, what I aammmm... |
| 443 | | |
| 444 | | What is reST ? |
| 445 | | ============== |
| 446 | | |
| 447 | | Several remarks: |
| 448 | | |
| 449 | | - the reST syntax doesn't obfuscate the text |
| 450 | | - punctuation signs are used to underline section titles |
| 451 | | |
| 452 | | Part 2 |
| 453 | | ====== |
| 454 | | |
| 455 | | Writing documents in reST |
| 456 | | |
| 457 | | - `What is reST ?` |
| 458 | | - **reST syntax overview** |
| 459 | | - `reST tools` |
| 460 | | |
| 461 | | reST syntax overview |
| 462 | | ==================== |
| 463 | | |
| 464 | | - punctuation signs to underline sections |
| 465 | | - text enhancements |
| 466 | | |
| 467 | | - ``*emphasis*`` |
| 468 | | - ``**strong emphasis**`` |
| 469 | | - ```interpreted text``` |
| 470 | | - ````inline literal text```` |
| 471 | | |
| 472 | | reST syntax overview |
| 473 | | ==================== |
| 474 | | |
| 475 | | - bullet list: use +, * or - |
| 476 | | - enumerated list: use n. (1., 2., etc.) or #. (automatic) |
| 477 | | |
| 478 | | Part 2 |
| 479 | | ====== |
| 480 | | |
| 481 | | Writing documents in reST |
| 482 | | |
| 483 | | - `What is reST ?` |
| 484 | | - `reST syntax overview` |
| 485 | | - **reST tools** |
| 486 | | |
| 487 | | reST tools |
| | 410 | - **Partie 2: les doctests** |smile| |
| | 411 | - `Partie 3: le DDD` |
| | 412 | |
| | 413 | Partie 2 |
| | 414 | ======== |
| | 415 | |
| | 416 | Les `doctests` |
| | 417 | |
| | 418 | - basés sur le principe du `literate programming` (KNUTH) |
| | 419 | - utilise le prompt Python |
| | 420 | |
| | 421 | Les doctests |
| | 422 | ============ |
| | 423 | |
| | 424 | Exemple de doctest `inline`:: |
| | 425 | |
| | 426 | def somme(a, b): |
| | 427 | """ calcul la somme |
| | 428 | |
| | 429 | >>> somme(1, 3) |
| | 430 | 4 |
| | 431 | >>> somme(2, 2) |
| | 432 | 4 |
| | 433 | """ |
| | 434 | return a + b |
| | 435 | |
| | 436 | |
| | 437 | Les doctests |
| | 438 | ============ |
| | 439 | |
| | 440 | |smile| Les exemples sont directement disponibles |
| | 441 | |
| | 442 | |sad| Le code devient dur à lire |
| | 443 | |
| | 444 | Les doctests |
| | 445 | ============ |
| | 446 | |
| | 447 | Solution: séparer les doctests dans un fichier texte |
| | 448 | |
| | 449 | - chaque module de code peut avoir son module de doctest |
| | 450 | - un script `execute` ces modules de tests |
| | 451 | |
| | 452 | Les doctests |
| | 453 | ============ |
| | 454 | |
| | 455 | Exemple de doctest séparé. Fichier *calc.py*:: |
| | 456 | |
| | 457 | def somme(a, b): |
| | 458 | """ calcul la somme |
| | 459 | """ |
| | 460 | return a + b |
| | 461 | |
| | 462 | Fichier *calc.txt*:: |
| | 463 | |
| | 464 | >>> from calc import somme |
| | 465 | >>> somme(1, 3) |
| | 466 | 4 |
| | 467 | >>> somme(2, 2) |
| | 468 | 4 |
| | 469 | |
| | 470 | Les doctests |
| | 471 | ============ |
| | 472 | |
| | 473 | Un fichier doctest devient un test unitaire grâce au module `doctest`:: |
| | 474 | |
| | 475 | import doctest |
| | 476 | import unittest |
| | 477 | |
| | 478 | def test_suite(): |
| | 479 | suite.append(doctest.DocFileTest('calc.txt')) |
| | 480 | return unittest.TestSuite(suite) |
| | 481 | |
| | 482 | if __name__ == '__main__': |
| | 483 | unittest.main(defaultTest='test_suite') |
| | 484 | |
| | 485 | Les doctests |
| | 486 | ============ |
| | 487 | |
| | 488 | doctests + tests classiques == campagne de test |
| | 489 | |
| | 490 | Le plan |
| | 491 | ======= |
| | 492 | |
| | 493 | - `Quelques définitions` |
| | 494 | - `Partie 1: le TDD avec Python` |
| | 495 | - `Partie 2: les doctests` |
| | 496 | - **Partie 3: le DDD** |smile| |
| | 497 | |
| | 498 | Partie 3 |
| | 499 | ======== |
| | 500 | |
| | 501 | Le Document-Driven Development |
| | 502 | |
| | 503 | Principe: |
| | 504 | |
| | 505 | Les doctests séparés peuvent `aussi` être des documents |
| | 506 | |
| | 507 | Documentation |
| | 508 | ============= |
| | 509 | |
| | 510 | Chaque doctest == alternance de code et d'explications:: |
| | 511 | |
| | 512 | Module calc |
| | 513 | |
| | 514 | Le module calc contient une fonction pour faire des sommes: |
| | 515 | |
| | 516 | >>> from calc import somme |
| | 517 | |
| | 518 | Cette fonction prends deux paramÚtres: |
| | 519 | |
| | 520 | >>> somme(1, 3) |
| | 521 | 4 |
| | 522 | >>> somme(2, 2) |
| | 523 | 4 |
| | 524 | |
| | 525 | Documentation |
| | 526 | ============= |
| | 527 | |
| | 528 | |smile| Promiscuité de la documentation |
| | 529 | |
| | 530 | |smile| La documentation évolue en même temps que le code |
| | 531 | |
| | 532 | |smile| Le développeur utilise la doc. pour concevoir les tests |
| | 533 | |
| | 534 | |smile| La documentation du projet est conçue en partie par ce biais |
| | 535 | |
| | 536 | Documentation |
| | 537 | ============= |
| | 538 | |
| | 539 | |important| Un doctest doit rester un document: ne pas le noyer dans des |
| | 540 | `test fixtures` (mise en place pour les tests) |
| | 541 | |
| | 542 | |important| Les doctests montrent des exemples **publics** de code |
| | 543 | |
| | 544 | |important| Les tests unitaires classiques s'occupent du reste |
| | 545 | |
| | 546 | Documentation |
| | 547 | ============= |
| | 548 | |
| | 549 | -> Demo: construction d'un module de calcul |
| | 550 | |
| | 551 | |
| | 552 | Documentation |
| | 553 | ============= |
| | 554 | |
| | 555 | Pour aller plus loin: |
| | 556 | |
| | 557 | -> utilisation du reSTructuredText dans les documents |
| | 558 | |
| | 559 | |
| | 560 | Conclusion |
| 490 | | XXX |
| 491 | | |
| 492 | | Part 2 |
| 493 | | ====== |
| 494 | | |
| 495 | | Writing documents in reST |
| 496 | | |
| 497 | | - `What is reST ?` |
| 498 | | - `reST syntax overview` |
| 499 | | - `reST tools` |
| 500 | | |
| 501 | | reST can be: |
| 502 | | |
| 503 | | - used for all documentation needs |
| 504 | | - manipulated like code (versioning, etc.) |
| 505 | | - easily transformed into various shapes |
| 506 | | |
| 507 | | Python developers |love| reST |
| 508 | | |
| 509 | | The plan |
| 510 | | ======== |
| 511 | | |
| 512 | | - `A few definitions` |
| 513 | | - `Part 1: doing TDD in Python` |
| 514 | | - `Part 2: writing documents in reST` |
| 515 | | - **Part 3: writing doctests** |
| 516 | | - `Part 4: doing DDD in Python` |
| 517 | | |
| 518 | | How to organise tests in a Python project |
| 519 | | ========================================= |
| 520 | | |
| 521 | | By the way, how Python code is organized ? |
| 522 | | |
| 523 | | - a package == a folder with files |
| 524 | | - a module == a file with classes, functions and variables |
| 525 | | |
| 526 | | Each package comes with |
| 527 | | |
| 528 | | - a `doc` subfolder |
| 529 | | - a `tests` subfolder <--- let's put our unit tests here |wink| |
| 530 | | |
| 531 | | Following the TDD principles: |
| 532 | | |
| 533 | | - one code module <==> one test module |
| 534 | | |
| 535 | | How to organise tests in a Python project |
| 536 | | ========================================= |
| 537 | | |
| 538 | | Our division package will look like this:: |
| 539 | | |
| 540 | | division |
| 541 | | | |
| 542 | | |-- division.py |
| 543 | | | |
| 544 | | |-- tests |
| 545 | | | | |
| 546 | | | |-- test_division.py |
| 547 | | | |
| 548 | | |-- doc |
| 549 | | | |
| 550 | | |-- division.txt <-- some cool doc |
| 551 | | |
| 552 | | Part 1 |
| 553 | | ====== |
| 554 | | |
| 555 | | `Doing TDD in Python` |
| 556 | | |
| 557 | | - `The TDD principles` |
| 558 | | - `How to write tests in Python` |
| 559 | | - `How to organise tests in a Python project` |
| 560 | | |
| 561 | | |
| 562 | | Part 1 |
| 563 | | ====== |
| 564 | | |
| 565 | | Mettre une photo de matrix |
| 566 | | |
| 567 | | "I now TDD in Python now" -- Neo, The Matrix |
| | 563 | Questions ? |
| | 564 | |
| | 565 | Ressources |
| | 566 | ========== |
| | 567 | |
| | 568 | - http://docs.python.org (doctest et unittest) |
| | 569 | - http://programmation-python.org |
| | 570 | |
| | 571 | Ressources |
| | 572 | ========== |
| | 573 | |
| | 574 | Sortie le 16 aout: |
| | 575 | |
| | 576 | .. figure:: media/guide.png |
| | 577 | :align: center |
| | 578 | :height: 300 |
| | 579 | :width: 200 |