How to publish your Robot Framework library to PyPi
In this short tutorial I will explain how to create a Robot Framework library and making it public by publishing it on PyPi. I will use RobotNotifications as an example. A small library I created to post custom messages to a Slack or Mattermost channel.
Creating a Robot Framework test library
Importing Custom Keywords
Creating a custom library is very easy since Robot Framework can just import your Python functions and methods as Robot Framework keywords.
In the following example Robot Framework will import only the post_message_to_channel
method, and will exclude all methods starting with an underscore.
The keyword name within Robot Framework will be “Post Message To Channel”
Libraries implemented as classes can take arguments.
In this example we can use the “Post Message To Channel” keyword as follows:
- Importing the Library with the webhook as argument
- Using the keyword with the positional text argument, and optional keyword arguments
Test library scope and version
By using class attribute ROBOT_LIBRARY_SCOPE
you can control when Robot Framework will create a new instance of your test library.
It’s possible to use the following values:
- TEST CASE
A new instance is created for every test case. This is the default. - TEST SUITE
A new instance is created for every test suite. - GLOBAL
Only one instance is created during the whole test execution.
The class attribute ROBOT_LIBRARY_VERSION
is used to define the version of your library.
Keyword Decorator
It’s possible to expose a different keyword name instead of the default method name by using the keyword decorator. Simply import the decorator and state the keyword name in it.
Preparing Your Library For Publication
When we have our Robot Framework library we can now start preparing it for publication on PyPi.
Setup.py
Before we can publish our package to PyPi we need to provide some information about it. We can do this with a setup.py
file.
- name
The name of your package on PyPi - version
The version of your package - description
The description of your package on PyPi - long_description
Specify the README file with documentation about your package - packages
The package that needs to be included - install_requires
A string or list of strings specifying what other packages need to be installed when this one is.
README.md
Before we will publish our Robot Framework library we need to create some documentation. For this example I used a markdown file, but documentation in reStructuredText is also possible on PyPi.
PyPi will display this README by including the README in the long_description
in the setup.py
Publishing your library to PyPi
Now the real publishing begins!
Create account PyPi
First we need a account on PyPi.
For testing the publishing part its advised to try this first on TestPyPi.
Create account TestPyPi:
https://test.pypi.org/account/register/
Create account PyPi:
https://pypi.org/account/register/
Building
Let’s say we have the following project setup:
RobotNotifications
__init__.pyREADME.MD
setup.py
We need to build our package for publication on PyPi. We can do that with the following command:
python setup.py sdist bdist_wheel
This will create a new directory dist
with a source archive and a wheel.
For testing and publishing the package we need Twine.
Install Twine:
pip install twine
We can test if the package description will render correctly on PyPi by checking the contents in the dist
directory with the following command:
twine check dist/*
Uploading
Now it’s time to upload the package!!
First upload it on TestPyPi:
twine upload --repository-url https://test.pypi.org/legacy/ dist/*
This command will ask you for your username and password you created earlier on TestPyPi. After the upload succeeded head over to TestPyPi to view your uploaded package.
The page should display your README and the information you gave in the setup.py
file.
If everything looks good you can upload the package to PyPi:
twine upload dist/*
That’s it!
Your Robot Framework library is now available for everyone, they can just pip install your library:
pip install YOUR-LIBRARY-NAME