Install
// Installs packages from requirements.txt
$ pip install -r requirements.txt
// Saves currently installed requirements to requirements.txt
$ pip freeze > requirements.txtTechnology
- CirlceCI
- flake8 for linting
- Flask
- SonarCloud - for static analysis, including coverage
- Coverage.py for coverage using unittest
- 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/activateSetting up Project (venv) in PyCharm
CMD + ,to open the system preferences.Project: <project name>tab on the left.> Project Interpreterto select the interpreter> Settings > Addto add the virtual environment- Select
Existing environmentand if not auto-populated, navigate tovenv/bin/python - 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
- Has built-in functions for things such as
store_test_results, which only requires thepathparameter. - Similar to most other pipeline languages, written in yaml (GitHub Actions)