Skip to content

fishey2/python-component-template

Repository files navigation

Python Component Template

Fishey2 Quality Gate Status

SonarCloud Coverage SonarCloud Bugs SonarCloud Vulnerabilities

Install

// Installs packages from requirements.txt
$ pip install -r requirements.txt

// Saves currently installed requirements to requirements.txt
$ pip freeze > requirements.txt

Technology

  1. CirlceCI
  2. flake8 for linting
  3. Flask
  4. SonarCloud - for static analysis, including coverage
  5. Coverage.py for coverage using unittest
  6. make - used in place of dojo

Process

Create Virtual Environment

// Pip could also be pip3 depending on your default installation of python
$ pip install virtualenv

// Create a virtual enviornment
(Project Root)$ virtualenv venv

// Activate the virtual environment
(Project Root)$ source venv/bin/activate

Setting up Project (venv) in PyCharm

  1. CMD + , to open the system preferences.
  2. Project: <project name> tab on the left.
  3. > Project Interpreter to select the interpreter
  4. > Settings > Add to add the virtual environment
  5. Select Existing environment and if not auto-populated, navigate to venv/bin/python
  6. Select and Apply

Testing

UnitTest Example

An example of a test case in Python

import unittest


class TestHelloWorld(unittest.TestCase):

    def test_true(self):
        self.assertTrue(True)


if __name__ == '__main__':
    unittest.main()

To run this you can use python -m unittest discover test/, the default.

Packages

__init__.py denotes that it is a package allows you to group modules to export.

The following example is used in this project to keep the same structure between src and test.

#!/usr/bin/env python

from distutils.core import setup

setup(name='component_template',
      version='1.0',
      description='Python Component Template',
      author='Brett Fisher',
      author_email='brett.fisher@thoughtworks.com',
      packages=['component_template'],
      package_dir={'component_template': 'src/component_template'}
      )

pip install . will install the source using the information provided. This can then be used as an import to the tests.

import unittest
from component_template import helloworld


class TestHelloWorld(unittest.TestCase):

    def test_hello(self):
        self.assertTrue(helloworld.hello())


if __name__ == '__main__':
    unittest.main()

Adding xmlrunner required no changes to the tests themselves, and can instead be achieved from the command line. xmlrunner outputs an JUnit type xml output from the tests.

Circle CI

  1. Has built-in functions for things such as store_test_results, which only requires the path parameter.
  2. Similar to most other pipeline languages, written in yaml (GitHub Actions)

About

A component template for microservices (Flask, SQL Alchemy, Swagger)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published