13. Code should be readable
Avoid comments but avoid obvious ones (the code
should be self-explanatory)
Avoid comments too verbose
Comments need to be maintained
15. The Zen of Python (PEP20)
Beautiful is better than ugly
Explicit is better than implicit
Simple is better than complex
Complex is better than complicated
Readability counts
16. Coding Style: Readability Counts
“Programs must be written for people to read, and
only incidentally for machines to execute.”
Abelson & Sussman, Structure and Interpretation of Computer Programs
17. PEP8: Style guide for Python
Indentation
Maximum line length
Blank lines
Redundant white spaces
Imports
Comments
Naming conventions
…
18. How to check /enforce PEP8
Pycharm (Tools > Flake8)
Git pre-commit hook (http://goo.gl/bteYkq)
Jenkins job for continuous integration
22. Naming best practises
Adopt a convention and stick with it (fetch vs. retrieve vs. get)
Use intention-revealing names
Avoid misleading readers
Make meaningful distinctions
Use searchable names
Avoid mental mapping
Use domain names
From Clean code, R. Martin
33. Main principles
Split components into repositories
Web app: use clean layers
view / endpoint (presentation)
service (business logic)
DAO (model)
Use message bus for communication inter-components (i.e. RabbitMQ)
Use daemons / workers for long-running tasks
Use events / signals to decouple unrelated actions
Microservices
34.
35. Architecture at Cloudlock
Nginx
AWS VPC
Web
Server
API
Server
Message Bus (RabbitMQ)
RDS
PostgreSQL
ElastiCache
Redis
Authentication Gateway
Worker Worker Worker
Logs
42. Different types of tests
Manual Test
Unit Test
Integration Test
System Test
Acceptance/Regression Test (end-to-end)
Smoke Test
43. Unit Test
Test if small, distinct and isolated portions of code
are working as expected, provided some input
Mock your dependencies (database, logger, service)
Should be very fast
No need to test EVERYTHING (i.e. private methods)
Frameworks: unittest, pytest and nose
44.
45. Pytest
No need for base class (works both for classes and methods)
More pythonic than unittest
Fixtures (conftest.py)
Parametrized tests
Markers
Various plugins for coverage, bdd, django, twisted…
Used for unit tests, integration tests and end to end tests
46. Integration Tests
Test communication between services
You may mock some of the dependencies depending on
what you test
47. System Tests
Global test, more functional of the whole system
TestApp (Pyramid app) using a test database
Call the API and test the expected response code
48. End-to-end tests
Splinter: web driver abstraction (firefox, selenium)
splinter-pytest: fixtures for browser
Plugin for rerun flaky tests, tests with steps
Use Selenium Grid to test several browsers
49. Smoke Tests
Make sure the system is alive
Tests that the components are working
Tests that the components can communicate
Health check / status page
52. Code Quality Metrics
Code Standards Violations (pylint /flake8/pyflakes)
LOC (Lines Of Code)
Cyclomatic Complexity
Unit Tests Coverage
53. Code Coverage
Makes sense only for unit tests
Calculates the coverage based on the paths run by
the tests
100% coverage is not a goal on its own
May be misleading regarding the code quality
63. Continuous Integration Flow
Feature Branch
Develop Branch
Git Push
Updates
Github PR
Several jobs:
- pep8 compliance
- unit test
- integration test
- end to end test
Merge PR
Pull Request
Tag
Deployment
Code Review