botbotbot 's blog

Google Compute Engine

Open Port On Google Compute Engine

  1. Go to cloud.google.com
  2. Go to my Console
  3. Choose you Project.
  4. Choose “Compute Engine”
  5. Choose “Networks”
  6. Choose the interface for you Instances.
  7. In firewalls section, Go to Create New.
  8. Create your rule, in this case “Protocols & Ports” would be “tcp:9090” i guess.
  9. Save.

Attach Disk

Mock

Mock - Mocking and Testing Library

If you mock some thing, it can’t be runnable. Don’t forget it just mock object.

Mock Method that can be runnable

# side_effect will make mock method can be runable  
# this can be use to reutrn_value in different arguments
>>> mock_method = mock.Mock()  
>>> mock_method.side_effect = lambda x: x + 1  
>>> mock_method(1)
2
>>> mock_method(2)
3

Patch Module Attribute

# Using PropertyMock with return_value argument  

>>> with mock.patch('package.module.attribute',  
                     new_callable=mock.PropertyMock(return_value=3))
                     as mock_attribute:  
>>>       print(mock_attribte)  
>>>       print(package.module.attribute)  
3  
3  

my_mock.mock_calls

Reference:

  1. The Mock Class
  2. enhancing-python-unit-tests-with-more-decorators
  3. python-decorators-with-parameters

Watchdog

Watchdog

Python API and shell utilities to monitor file system events.

pip install watchdog

Watching python file changes

Watchdog over Docker (VM) via polling - Ref

from watchdog.observers.polling import PollingObserver as Observer
Observer() #polling every 1 second

References:

  1. Event Handler Classes
  2. Watching a directory for file changes
  3. Continuous integration in python using watchdog

Continuos Integrations

Continuous Integrations doesn’t get rid of bugs, but it does make them dramatically easier to find and remove. - Continuous Integration by Martin Fowler

Own Sever Services

Online Services

Python Testing

There are difference between ‘building it right’ and ‘building the right thing’. -Python Testing Cookbook by Greg L. Turnquist

Good Test Habits

  • A few test is better than nothing
  • Coverage isn’t everything
    • make working software not a 100% coverage
  • Capturing a bug in an automated test
    1. Write Tests for the bug
    2. Add it to your test suite
    3. Fix the bug
    4. Veriry test suite passed
  • Testing Concept
    1. AAA - Arrange Act Assert
    2. BDD - Give When Then

Tips:

  • Run All unittest in module:

    python -m unittest discover <test_module>
    
  • Run nosetests with python3.
    • Install nose into python3 package with pip3 then you can run nosetests with python3 via nosetests-3.x ex. nosetests-3.4 if your python3 is version 3.4
    # ubuntu install python3-pip first
    # $ sudo apt-get install python3-ip
    $ pip3 install nose
    $ nosetests-3.x
    
  • Problems using nose in a virtualenv

    you can’t run nose in system-packages to using packages in virtualenv (sandbox)

  • Run PDB with nosetest:

    from nose.tools import set_trace; set_trace()
    

Nose Multiprocess

You should make unittest run faster with mocking better than using multi processes.
There are many problem with nose plugin ex. rednose, coverage, xunit.

In worst case more processeses can make your tests slower depends on your tests.

$ nosetests --processes=NUMBER_OF_PROCESSORS --process-timeout <sec>

Plugin Problem:

  1. Rednose - disable rednose by remove –rednose option and NOSE_REDNOSE in enviorment variable
  2. coverage - fix by nose-cov
  3. xunit - fix by nose_xunitmp

Test Coverage

Stmts - statements
Miss - statements that not executed
Cover - (Stmts - Miss)/Stmts * 100

  • Coverage html
    • Generate an Pretty HTML report
    • powerful way to visually inspect the code and see the lines that missing
    nosetests --with-coverage --cover-html 
    
  • Coverage erase

    coverage erase
    coverage -e
    nosetests --with-coverage --cover-erase
    
  • Coverage with filter Third-party Libraries out

    nosetests --with-coverage --cover-package=<package,package2>
    nosetests --with-coverage --cover-package=$(ls | grep '.py$' | sed 's/[.]py$//' | xargs | sed 's/[\ ]/,/g')
    

Mock

  • mocking pattern

    1.record/replay pattern
    2.action/assert pattern

  • mock tests behavior
    • test the method was called or not called by assert_called_with(), etc.
  • stubs tests state
    • recording method class and its result

Behavior Driven Development

  • BDD - Given, When, and Then
  • BDD Stroy
    -Feature
    – Scenario
    — steps

  • TDD vs BDD
    • setUp == Given
    • call unit == Then
    • assert == When
  • Lettuce is a Cucumber-like BDD tool built for Python.

Robot Framework - Acceptance Testing

  • Acceptance testing is block box testing
  • Customers are usually more interested in what the software does, not how it does it.
  • Robot Framework uses keywords to define tests, test steps, variables, and other testing components and need to maps Robot Framework keywords to our Python code
  • Robot Framework uses data-driven test suite

Jekin(Java) - Continuous Integration

  • nosetests –with-xunit
  • tests upon commit(trigger),when scheduled

Tools:

pip install nose rednose coverage
echo "export NOSE_REDNOSE=1" >> .zshrc #enable rednose
pip install watchdog
  • Tox - multiple environments testing
pip install tox

Testing Python

  1. Python Testing Start Here
  2. Testing in Python: using nose & mocks
  3. Mock - Mocking and Testing Library
  4. An Introduction to Mocking in Python
  5. INTRODUCING BDD
  6. Mock Like a Boss
  7. Beginning Test-Driven Development in Python
  8. Behavior-Driven Development in Python
  9. Testing with mock
  10. Software Testing Course
  11. Patching Mocks in Python
  12. Python Nose: Speed up the runner
  13. Beginning Test-Driven Development in Python
  14. python-test-double
  15. Mocking in Python - How to Bypass Expensive and External Code Within Your Tests