Integration Test and Functional Test in plone testing explained

Preface

As a plone developer, I have to do writing lots of unittests for our projects, so I had concerned on plone testing documentation from plone’s official site. Usually I learnt from there about two types of testing, `Integration Testing` and `Functional Testing`. Also knew about Testing Layer and how layer fixture is used while making above kind of tests, in other words which layer fixture is appropriate for which testing, i.e for Functional Testing should be used Functional Testing Layer fixture.

Integration Testing Fixture is generally creating a transaction for each test and just abort the transaction after test being done. Single database(demo storage) is whole test layer lifecycle,  there is not possible to do any persistent activity (transaction commit) in side tests code. Beside Functional Testing Fixture provides database (demo storage) for each test and removed during test teardown (making a stack of database for each test and pop up during tear down process), full transaction lifecycle is happened here, even you could do manually commit and/or use of sub-transaction as well.

By naming (Integration Testing, Functional Testing ) and characteristics  of both, I got confused! my thinking was integration test means collection of components (could be some functions from a module or coming from other modules or api methods from different packages) whether working together perfectly, so here we need persistent features (transaction commit) as we might need to add fixture data and should be available outside of session. For functional test, i thought that this about single component (for example: test single function or method of class), so here we don’t need transaction features, but I was completely wrong (my thought was so stupid)

To get rid out of my confusion, I have planned to dig down into online resources. Very first  I am trying understand that what are meaning of Integration test and Functional test , so ask google and found out from stackoverflow what I was looking for! about Functional testing:

Functional testing is when you test the system against the functional requirements of the product. Product/Project management usually writes these up and QA formalizes the process of what a user should see and experience, and what the end result of those processes should be. Depending on the product this can be automated or not.

Now i have better about those terms and why need persistent feature for Functional Testing

When and where should use each testing type

Integration Testing Fixture

This is relatively faster, you should use it as much as possible, until your test needs (commit) persistent feature. Ideal for testing of methods/functions, where you want to test how those are working together.

Functional Testing Fixture

Primary use case is, when you need to test/monitor after commit reaction of each function/component and make sure data goes to database successfully. If you have SqlAlchemy’s transaction integration, this functional testing fixture is mandatory for you, as some exception/error is thrown by SqlAlchemy during data going to be committed. i.e Integrity violation. Another required case is, when your test will need to use test browser, there you must need completion of transaction lifecycle to avail saved data during assertion. Test browser to be working,  you have to use ZServer Layer Fixture (from plone.app.testing) that is making test server for you.

Naturally Functional testing is slower compared to Integration testing , even it will be getting more slower if you attach ZServer fixture. In my previous experiences, i made functional test fixture with integrating zserver fixture and use this fixture for all of tests (i didn’t care if persistent(transaction commit) is required or not, test browser is needed or not!. ), one of cause, there was my lack of knowledge as result small test took so much time. Now i am learning and have some opinion from me! You should make two functional testing fixture, one without zserver fixture and one with zserver. So if any test needs test browser you will go for with zserver fixture. Bellows are example

from plone.testing import z2
from plone.app.testing import FunctionalTesting
from plone.app.testing import IntegrationTesting

MY_FIXTURE = MyFixtureLayer()
MY_INTEGRATION_TESTING = IntegrationTesting(
 bases=(MY_FIXTURE,),
 name='MyFixtureLayer:IntegrationTesting'
)
MY_FUNCTIONAL_TESTING = FunctionalTesting(
 bases=(MY_FIXTURE, ),
 name='MyFixtureLayer:FunctionalTesting'
)
MY_ZSERVER_FUNCTIONAL_TESTING = FunctionalTesting(
 bases=(
 MY_FIXTURE,
 z2.ZSERVER_FIXTURE),
 name='MyFixtureLayer:ZServerFunctionalTesting'
)

 

Anaconda vs Sublimelinter with Jedi for SublimeText

SublimeText is undoubtedly one of the most popular editor right now over million of software / web developers. The developer is using SublimeText as IDE usually, rather than using as plain text editor,  by making  IDE like features with the help from some great packages  available in free of cost.  For python development environment, Anaconda is all-in-one package for transforming to be IDE, in the other hand Sublimtelinter with Jedi combination one of the best method for enabling sublimetext to provide IDE like features. Both two have pros and cons, i will discuss bellows about my experiences for both.

Anaconda

Pros

  1. Most importantly, this all-in-one kind package and with no dependency, that means you don’t have headache to install multiple packages and separate configurations.
  2. Easy configuration and well documented.
  3. Numbers of active contributors and continuous development, see details
  4. Very faster linting process after introducing json server.
  5. McCabe code complexity checker

Cons

  1. Some time I discovered old problem come again with latest version, that is hanging on my sublimetext (may be one of the cause could be for big file (more than ten thousands of line)).
  2. There is no support for flake8, see discussion here. Although  anaconda linting is working with pyflake and pep8 combinely means likely to be same as flake8, but more just pyflake and pep8, flake8 has lots of cool plugins, you will miss those.
  3. Working with json server might easy, depends on Operating System you are using.

Sublimelinter and Sublime-Jedi

Pros

  1. This combination approach is like that you gather all good packages and using it. Sublimelinter is the best linting framework for SublimeText, in the other hand Jedi is one the best autocomplete library that is welly ported to Sublime, thanks author of SublimeJedi
  2. Jedi provides nice (CTRL+SHIFT+G) explorer (Go Defination) feature, you could go to the reference import very quickly, beside you could easily add extra python package paths those will be auto included to Jedi’s sys path.
  3. Sublimelinter is framework for linting, there are lots of plugins available, for python I would like to use flake8 but you may keen to use pylint, that is also available.
  4. Sublimelinter is working very fast and also has lots of configuration options, so that you could customize as your wish.

Cors

  1. I don’t find so much negative side to using this combination. But I find there is not much development activity on Github.
  2. Some rare case, jedi might not working (not sure if it is caused by Sublimetext or jedi itself)

 

We saw the simple comparison between two approach. In my opinion for novice user, they could go with Anaconda as this is very easy to adapted and configure. But in contrast of beginner user of sublimetext, I would suggest, professional developer should definitely with go with Sublimelinter specially if he/she is a full stack developer as it (Sublimelinter) has variety of plugins for different language/markup i.e css, javascript, html, json, yml and more.

Make Anaconda powered SublimeText as powerful Python IDE for full stack development

Like so many SublimeText lovers, Me as a python developer, it is also one of the favorite editor, I am using but I would like to use  it as full featured IDE. You will find many articles, blogs about how SublimeText can be transformed as pure IDE, but here i will discussed about the most easy way to turn your sublimetext as IDE for full stack python development.

Anaconda  is the single package that is capable to make your editor (SublimeText) as IDE for python development. It has almost all IDE like features, i.e linting, autocomplete, etc. You could say this is all-in-one package, with no third party dependency like SublimeLinter . One of the positive things that it is in continuous development by involvement of many contributors, so you can report any bug/error and the highest possibility, we will get the solution. They have very good documentation as well.

Install and Configuration is easy enough, I would suggest you follow instruction  from their official site, for advanced configuration you could follow here. It is good practice that you make user specific settings those are common for all your projects. Follows are example user settings.

{
    "auto_formatting": true,
    "auto_python_builder_enabled": false,
    "anaconda_linting": true,
    "anaconda_linter_phantoms": false,
    "validate_imports": true,
    "enable_signatures_tooltip": false,
    "merge_signatures_and_doc": false,
    "pep8_max_line_length": 120
}

If you saw the example configuration above, I intentionally set the value of  `pep8_max_line_length` is 120 (default is 80), in the other hand you will see that i don’t mention the path of python interpreter because it should goes on project specific settings (*.sublime-project) if you use individual interpreter(i.e virtualenv) for each project.

{
	"build_systems":
	[
		{
			"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
			"name": "PRS:: Anaconda Python Builder",
			"selector": "source.python",
			"shell_cmd": "{virtualenv path}/bin/python -u \"$file\""
		}
	],
	"folders":
	[
		{
			"path": "."
		}
	],
	"settings":
	{
		"python_interpreter": "{virtualenv path}/bin/python"
        }
}  

If you are using buildout for your project development, we have good news for you! There is a recipe plone.recipe.sublimetext for anaconda settings, this recipe will do for you to generate project specific settings. Followings are possible buildout settings for you (but you could do add  more custom option)

[buildout]
parts += 
     sublimetext
[sublimetext]
recipe = plone.recipe.sublimetext
eggs = ${instance:eggs}
anaconda-enabled = True
anaconda-linting-enabled = True
anaconda-completion-enabled = True
anaconda-validate-imports = True

 

Essential IELTS Vocabularies

Followings are list of vocabularies, I got those while doing study for IELTS examination.

  1. explore : to find out more information
  2. keen : interested
  3. potential :  possibility
  4. nutrients : chemical (food)
  5. penetrate : to drill through
  6. teeming : filled
  7. disoriented : disabled
  8. toxin : poison
  9. devour : eat
  10. ruled out : proven impossible
  11. hostile : unfriendly
  12. trassocemic : across ocean
  13. conventional : long held
  14. creeping : moving
  15. unfettered : uncontrolled ; The impact of unfettered population growth is clear for all to see.
  16. evident : clear / easily seen ; Urbanization is now a more evident worldwide phenomenon.
  17. scarcity : shortage / lack
  18. habitation: residency / to live
  19. impact: effect
  20. utilization: usage
  21. theories: ideas / proposals
  22. phenomenon : occurrence ; Urbanization is now more evident worldwide phenomenon than previously.
  23. developments: improvements
  24. desertification: process of becoming desert
  25. inhabit: dwell
  26. unimaginable: inconceivable
  27. predict: forecast
  28. fervent: showing strong feelings
  29. teeming with: to be full of
  30. vast: ample / immense
  31. commercial: concerning business
  32. precious: prized
  33.  portable: easily transported
  34. commodity: product
  35. currency: money in terms of paper 2 coins
  36. barter: trade / exchange
  37. repels: opposes / hold back
  38. fueled: encouraged / incited
  39. gabelle: salt tax
  40. deficiency: inadequacy
  41. punished: penalized
  42. inadequate: not enough
  43. off world: outside the world
  44. far-flung: far away
  45. volunteering: offering one’s services
  46. knock-on effect: domino effect
  47. function: act / work
  48. expertise: ability
  49. favourably: approvingly
  50. prerequisites: requirements
  51. elude: to escape
  52. attributes: features
  53. detracts: takes away
  54. constitute: comprise
  55. asset: advantage
  56. impart: grant / confer
  57. harnessed: excess
  58. embraces: accepts
  59. marine : found in or produced by sea ; Red and brown algae are almost exclusively marine.
  60. exclusively : solely ; Tiger brand shirt is exclusively available at supermarket in Copenhagen.
  61. terrestrial : earthbound ; Green algae are common in freshwater and in terrestrial situations.
  62. aid : help ; With the aid of a basic research grant from Forbairt.
  63. ideal : perfect / best
  64. stimulates : provokes/encourages : Propagate in culture under red light which stimulates reproduction in our growth rooms.
  65. preliminary : initial ; Preliminary results are very encouraging.
  66. relatively : comparatively ; Species of this genus are ideal for cross-breeding studies as the males and females are tiny filamentous plants that are relatively easy to grow.
  67. utilisation : usage ; Whilst dulse and carragreen  moss are worthy sea-vegetables with history of utilization.
  68. hybrids : mixed breeds ; A study of developing fast-growing hybrids of this species by crossing species from the Atlantic and Pacific is being carried out.
  69. consumption : utilization ; What increasingly should lead to greater consumption of sea -vegetables?
  70. potential : possible/probable
  71. panacea : a solution for all difficulties
  72. cited : quoted ; Small and Medium size companies will continue to be complacent about the RoHS transition until some major company is cited for non-compliance.
  73. comply : follow :Any country that can prove a product does not comply can levy fines against the vendor.
  74. levy : impose ; Any country that can prove a product does not comply can levy fines against the vendor.
  75. complacent : self satisfied ; Small and Medium size companies will continue to be complacent about the RoHS transition.
  76. hostile : unfriendly ; The EU has banned the use of six materials that are hostile to the environment.
  77. decline : decrease
  78. integral : basic/important ; RoHS must become an integral part of a designer’s development process.
  79. a host of : a group of / numerous ; A host of technical and reliability issues remain to be sorted out in Pb-free board processing.
  80. sorted out : solved; A host of technical and reliability issues remain to be sorted out in Pb-free board processing.
  81. looked at : gave importance
  82. constraint : limitation / restrict ; A new constraint revolving around environmental compliance.
  83. obsolescence : process of becoming out of date ; Manny Marcano cited the impact of parts obsolescence, including the need to redesign older products.
  84. vendors : sellers ; Electronics vendors worldwide are working to eliminate those substances.
  85. prohibited : forbidden
  86. insurmountable : cannot be dealt with ; But these obstacles are not insurmountable.
  87. remote : distant ; In the certain areas, say remote islands like the Outer Hebrides in Scotland.
  88. spur : motivation ; The environment will ultimately be the biggest spur.
  89. disparaged :  criticised / laughed at ; Video-conferencing has been disparaged for the lack of eye-contact.
  90. transmission : conveyance
  91. viable : attainable ; They were not viable application for general public use.
  92. enhance : improve
  93. cost-effective : saving money ; It is cost-effective for business to use.
  94. not complicated : straightforward ; Operating VC equipment is not complicated.
  95. expertise : ability ; The system can also be used to take expertise anywhere in the world.
  96. image : picture
  97. efficacy : effectiveness
  98. sparsely : present in small numbers
  99. shrink : decrease in size
  100. traits : characteristics
  101. less demanding : less difficult
  102. conventional : traditional ; The conventional view is that the domestication of wolves began between 10,000 and 20,000 years ago.
  103. endangered : threatened
  104. infer : understand
  105. companionship : friendship ; The fact that our companionship with dogs now appears to go back at least 100,000 years.
  106. veterinarians : doctor for animals
  107. co-existence : existing together
  108. rely on : depend on
  109. assertion : confident statement
  110. notable : noticeable
  111. elongated : long
  112. hoax : fake
  113. abstract :  unclear
  114. irrational : illogical
  115. embedded : covered
  116. resemble : to look alike
  117. abruptly : suddenly
  118. accelerate : to speed up
  119. inhibit : hinder
  120. anomalies : outliers
  121. enigma : person / thing that is mysterious
  122. intriguing : interesting
  123. varies : differs
  124.  consequences : results
  125. conjunction : connection
  126. degrade : worsen
  127. delusion : misconception
  128. disintegrate : break apart
  129. ramification : complications
  130. finite : limited
  131. counters : argues
  132. limitless : infinite
  133. concede : admit
  134. sustain : to maintain
  135. correlation : association

Make Sublime Text as the best IDE for full stack python development

Preface

Nothing says from me about Sublime Text 3, I assume you already  know about this nice editor. I can quote from realpython.com blog a very nice article about transforming editor to IDE, published there, I would recommend you look first at there.

Sublime Text 3 (ST3) is lightweight, cross-platform code editor known for its speed, ease of use, and strong community support. It’s an incredible editor right out of the box, but the real power comes from the ability to enhance its functionality using Package Control and creating custom settings.

Unlike above article (from realpython), this article is more specific tutorial like discussion, where you don’t need to more analysis about listed plugins, configurations (of course after starting you will do experiment with all, may find out something I did wrong or need improvement). The main target here, keep ST3 fast as it’s original but with sexy looking and featured with python based development tools and utilities. Bellows demonstration,  I got inspiration from that blog of course and also some of my real life experiences. In this writing, all settings, configurations are based on  Sublime Text 3  build 3126.

Install Sublime Text 3 (ST3)

Actually here nothing much to say about installation of ST3, because they have very good documentation and easy to install binary for all platform. If your OS is not Ubuntu, please ignore following step, because here discussion about install ST3 at Ubuntu in better way.

Good news that we have Ubuntu PPA for ST3, thanks to [@webupd8team] . I would recommend you to install ST3 from ppa, it will gives you lots of flexibility.  Steps are following:

  1. Open your terminal
  2. Add ppa : `~$ sudo add-apt-repository ppa:webupd8team/sublime-text-3`
  3. Install ST3: `~$ sudo apt-get update && sudo apt-get install sublime-text-installer`

Install Required ST3 Packages (Plugins)

First of all you have to install Package Control, please follow corresponding instruction and make sure it is installed. Followings are list of packages (some are required, some are optional and some are recommended)

  1. AdvancedNewFile
  2. AutoPEP8
  3. Git
  4. GitGutter
  5. Gitignore [optional]
  6. Solarized Color Scheme [optional but recommended for good looking]
  7. SideBarEnhancements
  8. SublimeJedi
  9. SublimeLinter
  10. SublimeLinter-annotations
  11. SublimeLinter-csslint
  12. SublimeLinter-flake8 [swipeable with pylint and should one of two]
  13. SublimeLinter-pylint [like no.10 should be choose one of two]
  14. SublimeLinter-html-tidy
  15. SublimeLinter-jshint [nice to have for web application, please follow it’s own documentation before active it]
  16. SublimeLinter-json
  17. SublimeLinter-pyyaml
  18. ProjectManager [optional]
  19. PackageResourceViewer [optional]
  20. Pandoc [optional]
  21. MarkdownEditing [optional]
  22. Markdown Preview [optional]
  23. Restructured​Text Improved [optional]

It is recommended that you install all packages using `Package Control` but you could also install manually, in that case please follow corresponding documentation.

Instruction for installing packages using Package Control

  1. Install Package Control if you haven’t yet.
  2. Use cmd+shift+P then Package Control: Install Package
  3. Search for  certain package, should be appeared in list and install it.
  4. You might need close ST3, to see all changes

Packages selection for `Sublime Text 3` has no restriction, I suggest you explore more packages as much as possible, but always keep in mind that many packages install at a time might enriched your Sublime Text‘s features but in the meantime will keep it busy too! as a result performance decreasing. Plugin/Package you should install and use, only if you know that why need it(package), how it is working, other than any unnecessary package you should avoid and also make sure different packages are not overlapping same feature.  Above listed packages are safe and necessary, but here also depends on development environment, you could add/remove packages from that list.

Note: some of packages need executable path is available at `PATH` variable to work, i.e flake8,  pylint, csslint, etc. Please follow each package documentation. 

Sublime Text 3 Settings

User Setting:

To find user settings, might vary from earlier version to new version. Example from `Sublime Text 3  build 3126` user settings is not directly available in menu, instead you will open default settings (`Preferences > Settings `),  two columns will be opened and one of this is user settings; beside earlier version you will find User Settings directly (`Preferences > Settings User`)

{
    "auto_complete_triggers": [{
        "characters": ".",
        "selector": "source.python"
    }],
    "color_scheme": "Packages/Solarized Color Scheme/Solarized (light).tmTheme",
    "font_size": 12,
    "ignored_packages": [
        "Markdown",
        "RestructuredText",
        "Vintage"
    ],
    "show_line_endings": true,
    "tab_size": 4,
    "translate_tabs_to_spaces": true,
    "trim_automatic_white_space": true,
    "trim_trailing_white_space_on_save": true
}

Above is the basic user settings, you could add more settings, documentation here

Sublime Jedi Settings

  1. Go to Preferences -> Package Settings -> Package Setting -> Jedi -> Settings User 
{
     "auto_complete_function_params": "required",
     "sublime_goto_layout": "single-panel"
}

More settings options could be found here

SublimeLinter Settings

  1. Go to Preferences -> Package Settings -> Package Setting -> SublimeLinter -> Settings User
{
    "user": {
        "debug": false,
        "delay": 0.25,
        "error_color": "D02000",
        "gutter_theme": "Packages/SublimeLinter/gutter-themes/Default/Default.gutter-theme",
        "gutter_theme_excludes": [],
        "lint_mode": "background",
        "linters": {
              "annotations": {
                   "@disable": false,
              },
              "csslint": {
                "@disable": false
              },
              "flake8": {
                   "@disable": false,
                   "max-complexity": 15,
                   "max-line-length": 120
              },
              "htmltidy": {
                "@disable": false
              },
              "jsl": {
                "@disable": false
              },
              "json": {
                  "@disable": false
              },
              "pyyaml": {
                 "@disable": false
              }
         }
    }
}

More about settings, you could find it’s own documentation.

AutoPep8 Settings

  1. Go to Preferences -> Package Settings -> Package Setting -> AutoPep8 -> Settings User 
{
    "max-line-length": 120
}

Known Issues

  1. Sublimelinter-flake8
  2. //platform.twitter.com/widgets.js

Per project settings (sublime-project file)

One of the great feature of Sublime Text is that you could make project specific editor settings (may you already know about it.),  more details here . If you are developing buildout based python project, `plone.recipe.sublimetext` for you!  this is good tool that will manage your project file more effectively.

LRU cache invalidation in python

I am getting sucked during writing one of unit test, because one specific function doesn’t works. After huge time spending, it is revealed that I used lru_cache that’s why it just execute one time (could be by another TestCase).

But in the test environment I need fresh result instead of serving from cache for various reasons.

After googling finally I found out real application of cache invalidation. Bellows are example

from functools import lru_cache
@lru_cache(maxsize=None)
def my_function(arg1, arg2)
   return arg1 + arg2
# Clear Cache before calling
my_function.cache_clear()
my_function(2, 2)

 

Create custom permission in Plone

How To Make Custom Permission [Addons Development]

As a Plone developer we have to deal with several types of permission, for example  `zope2.View`, `cmf.ModifyPortalContent`.  You can also make your own addons specific; you can do it easily from ZMI but I am going show the persistence way  (via zcml)

For example our zcml file name `permissions.zcml`

<configure xmlns="http://namespaces.zope.org/zope">

  <permission id="Your Permission Name as Unique ID"
              title="Your Permission Title ">
    <role name="Role Name"/>
    <role name="Role Name"/>
    ......................
  </permission>
  <permission id="myproduct.addProduct"
              title="My Product Add ">
    <role name="Manager"/>
    <role name="Site Administrator"/>
     ...................... 
   </permission>
</configure>

Activity:

Now add this file in your addon’s configure.zcml

Example:  <include file=”permissions.zcml” />