<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: JP Hutchins</title>
    <description>The latest articles on DEV Community by JP Hutchins (@jphutchins).</description>
    <link>https://dev.to/jphutchins</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F535749%2F76cce914-2f0c-497c-9940-c3f066ef4e2c.jpg</url>
      <title>DEV Community: JP Hutchins</title>
      <link>https://dev.to/jphutchins</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jphutchins"/>
    <language>en</language>
    <item>
      <title>GitHub Release Action for the Python Package Index</title>
      <dc:creator>JP Hutchins</dc:creator>
      <pubDate>Sat, 08 Jun 2024 22:39:58 +0000</pubDate>
      <link>https://dev.to/jphutchins/github-release-action-for-the-python-package-index-1m7n</link>
      <guid>https://dev.to/jphutchins/github-release-action-for-the-python-package-index-1m7n</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2mshzty71qs7nbw53syx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2mshzty71qs7nbw53syx.png" alt="workflow screenshot"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the &lt;a href="https://dev.to/jphutchins/building-a-universally-portable-python-app-2gng"&gt;first part of this series&lt;/a&gt;, we set up a repository for a universally portable Python app.  Today, we will register the package with &lt;a href="https://pypi.org/" rel="noopener noreferrer"&gt;PyPI, the Python Package Index&lt;/a&gt;, and use a GitHub Release Action to automate the distribution so that other Python users can install the app with &lt;code&gt;pipx&lt;/code&gt; or &lt;code&gt;pip&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/features/actions" rel="noopener noreferrer"&gt;GitHub Actions&lt;/a&gt; are automated routines that run on GitHub's sandboxed virtual machine servers, called "runners", and are (&lt;a href="https://docs.github.com/en/billing/managing-billing-for-github-actions/about-billing-for-github-actions" rel="noopener noreferrer"&gt;probably&lt;/a&gt;) free for your Public open source projects!&lt;/p&gt;

&lt;h2&gt;
  
  
  Security
&lt;/h2&gt;

&lt;p&gt;Let's first walk through the security threats that we will mitigate when deploying an app to PyPI.  Here is a list of threats that could put your users, and you, at risk:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;An attacker poses as a contributor and merges malicious code to your package via a &lt;strong&gt;Pull Request&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;An attacker hacks PyPI so that when a user tries to install your app they install a malicious package instead.&lt;/li&gt;
&lt;li&gt;An attacker logs in to your &lt;strong&gt;GitHub account&lt;/strong&gt; and replaces your app's repository with malicious code or uses a leaked &lt;strong&gt;Personal Access Token (PAT)&lt;/strong&gt; or &lt;strong&gt;Secure Shell (SSH) key&lt;/strong&gt; to push directly to the repository.&lt;/li&gt;
&lt;li&gt;An attacker logs in to your &lt;strong&gt;PyPI account&lt;/strong&gt; and replaces your package with malicious code.&lt;/li&gt;
&lt;li&gt;An attacker creates a malicious Python package with the &lt;strong&gt;same name&lt;/strong&gt; as yours and distributes it outside of PyPI.&lt;/li&gt;
&lt;li&gt;An attacker uploads a malicious Python package to PyPI with a name that is similar to yours ("typo squatting"), the intention being to &lt;strong&gt;trick users into downloading the wrong package&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;An attacker has compromised one of your upstream dependencies, a "supply chain attack", so that your users are affected when importing or running your package.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Once we've learned how to mitigate each of these risks, we will show how applying them would have prevented a recent supply chain attack in which impacted 170,000 Python users.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Reviewing Pull Requests
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Threat: An attacker poses as a contributor and merges malicious code to your package via a &lt;strong&gt;Pull Request&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;By default, GitHub will not allow any modification of your repository without your explicit approval.  This threat can be minimized by carefully reviewing all contributions to your repository and only elevating a contributor's privileges once they are a trusted partner.&lt;sup id="fnr-footnotes-1"&gt;1&lt;/sup&gt;   If you believe that a PR is attempting to inject a security vulnerability in your app, then you should &lt;a href="https://docs.github.com/en/communities/maintaining-your-safety-on-github/reporting-abuse-or-spam#reporting-an-issue-or-pull-request" rel="noopener noreferrer"&gt;report the offending account&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Vulnerability in the Package Repository (PyPI)
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Threat: An attacker hacks PyPI so that when a user tries to install your app, they install a malicious package instead.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;According to Stack Overflow's 2023 Developer Survey, &lt;strong&gt;45.32% of professional developers use Python&lt;/strong&gt;.&lt;sup id="fnr-footnotes-2"&gt;2&lt;/sup&gt;  Every industry and government in the world would be impacted by this threat and therefore has a financial incentive to keep PyPI secure.&lt;/p&gt;

&lt;p&gt;PyPI completed a security audit in late 2023 that found and remediated some non-critical security risks.&lt;sup id="fnr-footnotes-3"&gt;3&lt;/sup&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Authentication
&lt;/h3&gt;

&lt;p&gt;Threats #3-#6 all fall under the category of authentication: proving that your app, once received by your user, is an unmodified copy of your work - that it is &lt;em&gt;authentic&lt;/em&gt;.  Keep in mind that your user's trust is strengthened by your lack of anonymity.  If the application can be authenticated, then it can be permanently tied to your GitHub account, your PyPI account, then your email addresses, and ultimately, to &lt;em&gt;you&lt;/em&gt;.  Legal action can be taken against &lt;em&gt;you&lt;/em&gt;, which is a good reason not to distribute malware on PyPI.&lt;/p&gt;

&lt;p&gt;So, how can we prove that the software that a user receives when they type &lt;code&gt;pipx install jpsapp&lt;/code&gt; is authentic?  Let's look at each threat individually.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Protecting Your GitHub Account
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Threat: An attacker logs in to your &lt;strong&gt;GitHub account&lt;/strong&gt; and replaces your app's repository with malicious code or uses a leaked &lt;strong&gt;Personal Access Token (PAT)&lt;/strong&gt; or &lt;strong&gt;Secure Shell (SSH) key&lt;/strong&gt; to push directly to the repository.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Protection of your GitHub account web login is the same as it would be for any other sensitive website: use a strong password that is unique to the website (use a &lt;a href="https://bitwarden.com/resources/why-enterprises-need-a-password-manager/" rel="noopener noreferrer"&gt;password manager&lt;/a&gt;) and use two-factor authentication (2FA).&lt;/p&gt;

&lt;p&gt;There is a more direct path for an attacker to take, however, which is to obtain one of your SSH keys or Personal Access Tokens.&lt;/p&gt;

&lt;h4&gt;
  
  
  SSH Keys
&lt;/h4&gt;

&lt;p&gt;The starting point is that your SSH keys should never leave your device - not in an email, a text message, over a network share, and &lt;strong&gt;especially not as a commit to a remote repository&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Therefore, the threat is limited to the attacker gaining physical access to your PC.  Again, common mitigations come into play: enable your computer's lock screen after a short inactivity timeout, use a strong password, and enable full disk encryption.  The disk encryption is important in the event that your computer is stolen because it will prevent an attacker from accessing the contents of your disk by physically removing your storage drive and mounting it on their own machine.  In the event that an attacker does have physical access to your PC, you can still prevent the attacker from gaining access to your repositories by &lt;a href="https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/reviewing-your-ssh-keys" rel="noopener noreferrer"&gt;going to GitHub and revoking any SSH keys&lt;/a&gt; from the compromised computer.&lt;/p&gt;

&lt;h4&gt;
  
  
  Personal Access Tokens
&lt;/h4&gt;

&lt;p&gt;Personal Access Tokens (PATs) are an excellent way of providing temporary authentication to a repository from a computer that does not have access to your SSH key.  This is much better than simply sharing the SSH key since it prevents your SSH key from leaking.  PATs can be created with a specific security scope and include an expiration date.  However, even with all of these features in mind, creating many PATs and forgetting to delete them effectively leaks authentication all over the place - so delete them right after you no longer need them!&lt;/p&gt;

&lt;p&gt;In summary, keep your SSH keys and PATs secret and regularly audit your GitHub account to revoke access from any SSH keys or PATs that are no longer needed.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Protecting your PyPI Account
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Threat: An attacker logs in to your &lt;strong&gt;PyPI account&lt;/strong&gt; and replaces your package with malicious code.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Protection of your PyPI account web login is the same as it would be for any other sensitive website: use a strong password that is unique to the website (use a &lt;a href="https://bitwarden.com/resources/why-enterprises-need-a-password-manager/" rel="noopener noreferrer"&gt;password manager&lt;/a&gt;) and use two-factor authentication (2FA).&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Package Impersonation
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Threat: An attacker creates a malicious Python package with the &lt;strong&gt;same name&lt;/strong&gt; as yours and distributes it outside of PyPI.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;By default, tools like &lt;code&gt;pip&lt;/code&gt; and &lt;code&gt;pipx&lt;/code&gt; will search PyPI for the package specified.  To install a package from an outside source, &lt;code&gt;pip&lt;/code&gt; would need to be told explicitly to point to the location of the infected package.&lt;/p&gt;

&lt;p&gt;From the command line:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

pip install https://m.piqy.org/packages/ac/1d/jpsapp-1.0.0.tar.gz


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Or in &lt;code&gt;pyproject.toml&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight toml"&gt;&lt;code&gt;

&lt;span class="nn"&gt;[dependencies]&lt;/span&gt;
&lt;span class="py"&gt;jpsapp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="py"&gt;url&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"https://m.piqy.org/packages/ac/1d/jpsapp-1.0.0.tar.gz"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You can mitigate this threat by providing clear and explicit instructions about obtaining your package.  I recommend adapting this example and placing it prominently in your &lt;code&gt;README.md&lt;/code&gt; and other documentation.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;

&lt;span class="gu"&gt;## Install&lt;/span&gt;

&lt;span class="sb"&gt;`jpsapp`&lt;/span&gt; is &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;distributed by PyPI&lt;/span&gt;&lt;span class="p"&gt;](&lt;/span&gt;&lt;span class="sx"&gt;https://pypi.org/project/jpsapp/&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
and can be installed with &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;pipx&lt;/span&gt;&lt;span class="p"&gt;](&lt;/span&gt;&lt;span class="sx"&gt;https://github.com/pypa/pipx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;:
&lt;span class="p"&gt;```&lt;/span&gt;&lt;span class="nl"&gt;
&lt;/span&gt;pipx install jpsapp
&lt;span class="p"&gt;```&lt;/span&gt;&lt;span class="sb"&gt;


&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  6. Typo Squatting
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Threat: An attacker uploads a malicious Python package to PyPI with a name that is similar to yours ("typo squatting"), the intention being to &lt;strong&gt;trick users into downloading the wrong package&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For example, a user intending to install &lt;code&gt;matplotlib&lt;/code&gt; may make the typo &lt;code&gt;matplotli&lt;/code&gt; and accidentally install the wrong package.  In a sophisticated supply chain attack, the &lt;code&gt;matlplotli&lt;/code&gt; package would be mostly identical to the latest &lt;code&gt;matplotlib&lt;/code&gt; package with the only differences being obfuscated malware installation and execution.&lt;/p&gt;
&lt;h4&gt;
  
  
  Protect Yourself
&lt;/h4&gt;

&lt;p&gt;By making a typo when adding a dependency to your package, you could compromise not only your own PC, but the PC's of all your users.  Whenever possible, copy and paste the dependency name from the official documentation.  When in doubt, verify the package name directly at PyPI.org.&lt;/p&gt;
&lt;h4&gt;
  
  
  Protect Your Users
&lt;/h4&gt;

&lt;p&gt;It's impossible to completely mitigate one of your users making a typo, but you can make it easy for them to avoid the possibility altogether by providing clear and explicit instructions for installing your package as an application or as a dependency.&lt;/p&gt;

&lt;p&gt;When using code blocks in markdown documentation, it is preferred to put your code in blocks using three backticks rather than one.  This format makes it easier to select for copy and paste and GitHub will provide a clickable "copy" shortcut on the right side of the code block, as seen below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fag40abvp5kc1xv65nrag.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fag40abvp5kc1xv65nrag.png" alt="Screenshot of GitHub Adding a "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Make certain that the command you've added to the documentation is runnable as written.  For example, adding prefixes like &lt;code&gt;$&amp;gt;&lt;/code&gt; or &lt;code&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/code&gt; would make your command example unusable without modification and subsequently reintroduce the typo squatting threat.&lt;/p&gt;
&lt;h3&gt;
  
  
  7. Supply Chain Attack
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Threat: An attacker has compromised one of your upstream dependencies, a "supply chain attack", so that your users are affected when importing or running your package.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You've done everything right.  But, one day when you're updating your project's dependencies, you unknowingly infect your package and all of your users because one of &lt;em&gt;your dependencies&lt;/em&gt; fell victim to one of the threats described above.&lt;/p&gt;

&lt;p&gt;It is your responsibility as the package maintainer to select broadly-used and well-maintained dependencies for your application.  Be wary of packages that do not have recent commits (🌈&lt;em&gt;maybe they have no bugs&lt;/em&gt;🌈) or low user counts.  Always research a variety of options that meet your requirements and double check that Python does not have a &lt;a href="https://docs.python.org/3/" rel="noopener noreferrer"&gt;builtin&lt;/a&gt; that works for you!&lt;/p&gt;
&lt;h2&gt;
  
  
  Case Study: topggpy Supply Chain Attack
&lt;/h2&gt;

&lt;p&gt;On March 25th, 2024, Checkmarx broke the news of a successful supply chain attack that affected more than 170,000 Python users.&lt;/p&gt;

&lt;p&gt;Once infected, the attackers would have remote access to the user's Browser Data, Discord Data, Cryptocurrency Wallets, Telegram Sessions, User Data and Documents Folders, and Instagram Data.&lt;sup id="fnr-footnotes-4"&gt;4&lt;/sup&gt;  I suggest reading the entire &lt;a href="https://checkmarx.com/blog/over-170k-users-affected-by-attack-using-fake-python-infrastructure/" rel="noopener noreferrer"&gt;article&lt;/a&gt; before returning here to see how every aspect of the attack would have been mitigated by the strategies discussed above.&lt;/p&gt;
&lt;h3&gt;
  
  
  Protecting Your GitHub Account
&lt;/h3&gt;

&lt;p&gt;The primary failure for topggpy was editor-syntax's GitHub account being compromised.  Above, we discussed industry standard approaches utilizing password managers and two-factor authentication.&lt;/p&gt;
&lt;h3&gt;
  
  
  Reviewing Pull Requests
&lt;/h3&gt;

&lt;p&gt;editor-syntax was not the account owner of the topggpy &lt;a href="https://github.com/Top-gg-Community/python-sdk" rel="noopener noreferrer"&gt;repository&lt;/a&gt; yet had write access to the repository.  Granting a Collaborator write permissions while lacking the requirement for a Pull Request and Approval is a non-default GitHub Security configuration.  Remember that contributors do not need to have any special privileges to your repository in order to make Pull Requests from their own Fork.  When you are ready to add a Collaborator to your project, consider &lt;a href="https://docs.github.com/en/account-and-profile/setting-up-and-managing-your-personal-account-on-github/managing-user-account-settings/permission-levels-for-a-personal-account-repository" rel="noopener noreferrer"&gt;restricting their permissions&lt;/a&gt; to the bare minimum required for their role.&lt;/p&gt;
&lt;h3&gt;
  
  
  Authentication
&lt;/h3&gt;

&lt;p&gt;Delivery of the malware was accomplished by delivering users an inauthentic but fully functional version of the colorama package that would fetch, install, and execute the malware in the background simply by using the topggpy package as normal.&lt;/p&gt;

&lt;p&gt;If the changes that editor-syntax's compromised account made had been required to go through Pull Request and Approval, the attack would have been stopped.  The offending commit is &lt;a href="https://github.com/Top-gg-Community/python-sdk/commit/ecb87731286d72c8b8172db9671f74bd42c6c534" rel="noopener noreferrer"&gt;here&lt;/a&gt;:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="err"&gt;

&lt;/span&gt;&lt;span class="gd"&gt;- aiohttp&amp;gt;=3.6.0,&amp;lt;3.9.0
&lt;/span&gt;&lt;span class="gi"&gt;+ https://files.pythonhosted.org/packages/18/93/1f005bbe044471a0444a82cdd7356f5120b9cf94fe2c50c0cdbf28f1258b/aiohttp-3.9.3.tar.gz
+ https://files.pythonhosted.org/packages/7f/45/8ae61209bb9015f516102fa559a2914178da1d5868428bd86a1b4421141d/base58-2.1.1.tar.gz
+ https://files.pypihosted.org/packages/ow/55/4862e96575e3fda1dffd6cc46f648e787dd06d97/colorama-0.4.3.tar.gz
+ https://files.pythonhosted.org/packages/e0/b7/a4a032e94bcfdff481f2e6fecd472794d9da09f474a2185ed33b2c7cad64/construct-2.10.68.tar.gz
+ https://files.pythonhosted.org/packages/7e/86/2bd8fa8b63c91008c4f26fb2c7b4d661abf5a151db474e298e1c572caa57/DateTime-5.4.tar.gz
&lt;/span&gt;&lt;span class="err"&gt;

&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In this case it loads the tainted &lt;code&gt;colorama&lt;/code&gt; package from a non-PyPI, typo-squatted domain, &lt;code&gt;files.pypihosted.org&lt;/code&gt;, that was registered by the attackers to impersonate authentic packages.&lt;/p&gt;

&lt;p&gt;Note that &lt;code&gt;files.pythonhosted.org&lt;/code&gt; and &lt;code&gt;pypi.org&lt;/code&gt; are both authentic PyPI domains.  As discussed in Package Impersonation, package dependencies generally should not point to URLs and instead let the package manager resolve the resource.  Violation of this approach would have been immediately obvious during review of the Pull Request and the attack would have been thwarted.&lt;/p&gt;

&lt;h2&gt;
  
  
  Automated PyPI Publishing Tutorial
&lt;/h2&gt;

&lt;p&gt;Now that we've discussed some of the security risks involved in distributing a Python package, let's create a secure workflow that automates many of the tasks that would otherwise introduce opportunities for user error.&lt;/p&gt;

&lt;h3&gt;
  
  
  Create an Account at PyPI.org
&lt;/h3&gt;

&lt;p&gt;Create an account at &lt;a href="https://pypi.org/" rel="noopener noreferrer"&gt;PyPI.org&lt;/a&gt; and remember to use a strong password that is secured by a password manager and enable 2FA, as discussed above.&lt;/p&gt;

&lt;h3&gt;
  
  
  Add a Trusted Publisher at PyPI.org
&lt;/h3&gt;

&lt;p&gt;Once you are logged in to your account, select "Your projects" from the account dropdown in the upper right-hand corner.  Click on "Publishing" and scroll down to "Add a new pending publisher".&lt;/p&gt;

&lt;p&gt;Under the "GitHub" tab, fill out the fields following the example below.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;PyPI Project Name&lt;/strong&gt;: &lt;code&gt;jpsapp&lt;/code&gt;. Your package name as defined by the &lt;code&gt;name&lt;/code&gt; field of your &lt;code&gt;pyproject.toml&lt;/code&gt; and the directory name of the package.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Owner&lt;/strong&gt;: &lt;code&gt;JPHutchins&lt;/code&gt;. Your GitHub User or Organization name&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Repository name&lt;/strong&gt;: &lt;code&gt;python-distribution-example&lt;/code&gt;. The name of the repository as it is in the GitHub URL, e.g. &lt;code&gt;github.com/JPHutchins/python-distribution-example&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Workflow name&lt;/strong&gt;: &lt;code&gt;release.yaml&lt;/code&gt;.  The workflow will be located at &lt;code&gt;.github/workflows/release.yaml&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Environment name&lt;/strong&gt;: &lt;code&gt;pypi&lt;/code&gt;.  We will configure this environment at github.com&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Click "Add"&lt;/p&gt;

&lt;h3&gt;
  
  
  Define the Release Action
&lt;/h3&gt;

&lt;p&gt;A GitHub Release Action is a Workflow that is triggered by creating a release of your app.  For example, if you've made some important changes over a few weeks that you'd like your users to benefit from, tagging and releasing the new version of your app is the best way to accomplish it.&lt;/p&gt;

&lt;p&gt;Because this is free and open source software, there will be no fees for the cloud virtual machines provided by GitHub.&lt;/p&gt;

&lt;p&gt;All code snippets belong to the file &lt;code&gt;release.yaml&lt;/code&gt;, the complete version of which you can find &lt;a href=""&gt;here&lt;/a&gt;.  The original example is from &lt;a href="https://packaging.python.org/en/latest/guides/publishing-package-distribution-releases-using-github-actions-ci-cd-workflows/" rel="noopener noreferrer"&gt;this excellent article&lt;/a&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;

&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Release&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This will be the name displayed in the GitHub Actions interface.&lt;/p&gt;




&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;

&lt;span class="na"&gt;env&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;jpsapp&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This adds a variable to the workflow, accessible via &lt;code&gt;${{ env.name }}&lt;/code&gt;.  It is a simple convenience that allows the rest of this workflow definition to be reused in other repositories by simply changing the name on this line instead of throughout the file.&lt;/p&gt;




&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;

&lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;release&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;types&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;published&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Declares that the workflow should run whenever a new release is published.&lt;/p&gt;




&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;

&lt;span class="na"&gt;jobs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Everything indented under the &lt;code&gt;jobs:&lt;/code&gt; section are the definitions of the actions to perform.&lt;/p&gt;




&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;

  &lt;span class="na"&gt;build-dist&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;📦 Build distribution&lt;/span&gt; 
    &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Declare a job named &lt;code&gt;build-dist&lt;/code&gt; with friendly name "📦 Build distribution" that will run on an Ubuntu (Linux) runner.&lt;/p&gt;




&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;

    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Everything indented under the &lt;code&gt;steps:&lt;/code&gt; section are the steps to perform for this &lt;code&gt;job&lt;/code&gt;.&lt;/p&gt;




&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;

    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/checkout@v4&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This is almost always the first step in a job that will make use of the repository source code.  This may sound obvious, but it's best to be explicit about what resources are being made available, so it is required.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: if you are using the Git tag as the Single Source of Truth for your package version, then you'll probably need a step like &lt;code&gt;run: git fetch --prune --unshallow --tags&lt;/code&gt; to make sure that you have the latest tags on the runner.  See the more sophisticated build scripts and workflows of a real app, like &lt;a href="https://github.com/intercreate/smpmgr" rel="noopener noreferrer"&gt;smpmgr&lt;/a&gt;, for details.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;

    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/setup-python@v5&lt;/span&gt;
      &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;python-version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;3.x"&lt;/span&gt;
        &lt;span class="na"&gt;cache&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;pip"&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Setup Python using the default version and create a cache of the pip install.  The cache will allow workflows to run faster by reusing the global python environment installed by &lt;code&gt;pip&lt;/code&gt; in the next step, assuming that the dependencies have not changed since the cache was created.&lt;/p&gt;




&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;

    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;pip install .[dev]&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Install the development dependencies.  The workflow runs on a fresh Python environment, so we can simplify things somewhat by not using the venv.&lt;/p&gt;




&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;

    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;python -m build&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Build the &lt;strong&gt;sdist&lt;/strong&gt; and &lt;strong&gt;wheel&lt;/strong&gt; of your app.  The files generated by this kind of build system are called "artifacts", and these are the files that will be sent to PyPI.  &lt;/p&gt;




&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;

    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Store the distribution packages&lt;/span&gt;
      &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/upload-artifact@v4&lt;/span&gt;
      &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;python-package-distributions&lt;/span&gt;
        &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;dist/&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Upload the sdist and wheel, which are located at &lt;code&gt;dist/&lt;/code&gt;, as artifacts so that they are available for download in the GitHub Actions interface and easily accessible from the next &lt;code&gt;job&lt;/code&gt; using &lt;code&gt;actions/download-artifact&lt;/code&gt;.&lt;/p&gt;




&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;

  &lt;span class="na"&gt;publish-to-pypi&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Publish Python 🐍 distribution 📦 to PyPI&lt;/span&gt;
    &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;
    &lt;span class="na"&gt;needs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;build-dist&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;pypi&lt;/span&gt;
      &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;https://pypi.org/p/${{ env.name }}&lt;/span&gt;
    &lt;span class="na"&gt;permissions&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;id-token&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;write&lt;/span&gt;  &lt;span class="c1"&gt;# IMPORTANT: mandatory for trusted publishing&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Declare another job for the Ubuntu runner, &lt;code&gt;publish-to-pypi&lt;/code&gt;, with the friendly name "Publish Python 🐍 distribution 📦 to PyPI", that runs after the job &lt;code&gt;build-dist&lt;/code&gt; has completed successfully.&lt;/p&gt;

&lt;p&gt;This job also uses the environment, &lt;code&gt;pypi&lt;/code&gt;, that we created earlier, and defines the &lt;code&gt;url&lt;/code&gt; variable in the environment.  The url, &lt;code&gt;https://pypi.org/p/${{ env.name }}&lt;/code&gt;, will resolve to &lt;code&gt;https://pypi.org/p/jpsapp&lt;/code&gt;, and will be used by the &lt;code&gt;pypa/gh-action-pypi-publish&lt;/code&gt; step later in this job.  You can read more about environments on the &lt;a href="https://docs.github.com/en/actions/deployment/targeting-different-environments/using-environments-for-deployment" rel="noopener noreferrer"&gt;GitHub Docs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Finally, the job requires the &lt;code&gt;id-token: write&lt;/code&gt; permission to &lt;a href="https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/about-security-hardening-with-openid-connect#adding-permissions-settings" rel="noopener noreferrer"&gt;allow the OpenID Connect (OIDC) JSON Web Token (JWT)&lt;/a&gt; to be requested from GitHub by the &lt;code&gt;pypa/gh-action-pypi-publish&lt;/code&gt; action.  This OIDC token provides proof to PyPI that the package distribution upload is authentic; that is, it ties the release directly to the GitHub workflow run and thereby to your GitHub account. &lt;br&gt;
 This kind of temporary token authentication prevents using your GitHub or PyPI account credentials directly, which could create an opportunity for them to leak.&lt;/p&gt;




&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;

    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Download all the dists&lt;/span&gt;
        &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/download-artifact@v4&lt;/span&gt;
        &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;python-package-distributions&lt;/span&gt;
          &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;dist/&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Publish distribution 📦 to PyPI&lt;/span&gt;
        &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;pypa/gh-action-pypi-publish@release/v1&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The first step downloads the dists that were built and uploaded by the &lt;code&gt;build-dist&lt;/code&gt; job and the second step uploads those dists to the Python Package Index.&lt;/p&gt;




&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;

  &lt;span class="na"&gt;publish-dist-to-github&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;&amp;gt;-&lt;/span&gt;
      &lt;span class="s"&gt;Sign the Python 🐍 distribution 📦 with Sigstore&lt;/span&gt;
      &lt;span class="s"&gt;and upload them to GitHub Release&lt;/span&gt;
    &lt;span class="na"&gt;needs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;publish-to-pypi&lt;/span&gt;
    &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;

    &lt;span class="na"&gt;permissions&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;contents&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;write&lt;/span&gt;  &lt;span class="c1"&gt;# IMPORTANT: mandatory for making GitHub Releases&lt;/span&gt;
      &lt;span class="na"&gt;id-token&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;write&lt;/span&gt;  &lt;span class="c1"&gt;# IMPORTANT: mandatory for sigstore&lt;/span&gt;

    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Download all the dists&lt;/span&gt;
      &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/download-artifact@v4&lt;/span&gt;
      &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;python-package-distributions&lt;/span&gt;
        &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;dist/&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Sign the dists with Sigstore&lt;/span&gt;
      &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;sigstore/gh-action-sigstore-python@v2.1.1&lt;/span&gt;
      &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;inputs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;&amp;gt;-&lt;/span&gt;
          &lt;span class="s"&gt;./dist/*.tar.gz&lt;/span&gt;
          &lt;span class="s"&gt;./dist/*.whl&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Upload artifact signatures to GitHub Release&lt;/span&gt;
      &lt;span class="na"&gt;env&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;GITHUB_TOKEN&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ github.token }}&lt;/span&gt;
      &lt;span class="c1"&gt;# Upload to GitHub Release using the `gh` CLI.&lt;/span&gt;
      &lt;span class="c1"&gt;# `dist/` contains the built packages, and the&lt;/span&gt;
      &lt;span class="c1"&gt;# sigstore-produced signatures and certificates.&lt;/span&gt;
      &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;&amp;gt;-&lt;/span&gt;
        &lt;span class="s"&gt;gh release upload&lt;/span&gt;
        &lt;span class="s"&gt;'${{ github.ref_name }}' dist/**&lt;/span&gt;
        &lt;span class="s"&gt;--repo '${{ github.repository }}'&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This job signs the dists and uploads the dists, signatures, and certificates to the GitHub Release.  While it's best for your users to install your app via &lt;code&gt;pipx&lt;/code&gt;, this does allow users to verify the authenticity of the dists that are hosted on the GitHub Release page using &lt;a href="https://www.python.org/download/sigstore/" rel="noopener noreferrer"&gt;instructions&lt;/a&gt; provided by Python.&lt;/p&gt;




&lt;p&gt;Take a look the complete &lt;a href="https://github.com/JPHutchins/python-distribution-example/blob/e31907bec7a31e8ef7edc1dd33dfb10b6c0f496b/.github/workflows/release.yaml#L1-L87" rel="noopener noreferrer"&gt;release.yaml&lt;/a&gt; and use it as a template for your own applications or libraries.&lt;/p&gt;

&lt;h3&gt;
  
  
  Create the Release
&lt;/h3&gt;

&lt;p&gt;All of the hard work of automating the PyPI release process is out of the way and now it's time to deploy!&lt;/p&gt;

&lt;h4&gt;
  
  
  About Versioning
&lt;/h4&gt;

&lt;p&gt;When your application or library is ready for a release, the first step is tagging the version in some way.  PyPI is only going to care about the version line in your &lt;code&gt;pyproject.toml&lt;/code&gt;, while GitHub will want a Git tag for the release.  There may be many differences of opinion on &lt;em&gt;how&lt;/em&gt; to make sure that these match, but most will agree that &lt;strong&gt;these should match&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The simplest approach is to make a commit that bumps the version in &lt;code&gt;pyproject.toml&lt;/code&gt; with a commit message like "version: 1.0.1".  Immediately follow that up with a git tag that matches: &lt;code&gt;git tag 1.0.1&lt;/code&gt; and &lt;code&gt;git push --tags&lt;/code&gt; (use an annotated tag if you prefer), or create the tag from GitHub, as will be demonstrated below.  The downside here is that the lack of a Single Source Of Truth (SSOT) creates room for human error, or simply forgetfulness, when tagging release versions.&lt;/p&gt;

&lt;p&gt;For that reason, many approaches for establishing a SSOT have been developed, and you may find one that you prefer.  Some examples are the &lt;a href="https://github.com/tiangolo/poetry-version-plugin" rel="noopener noreferrer"&gt;poetry-version-plugin&lt;/a&gt; and &lt;a href="https://setuptools-git-versioning.readthedocs.io/en/v2.0.0/" rel="noopener noreferrer"&gt;setuptools-git-versioning&lt;/a&gt;.  &lt;a href="https://github.com/gitpython-developers/GitPython" rel="noopener noreferrer"&gt;GitPython&lt;/a&gt; can be used to enforce strict release rules.  The plugin will fill in the Python package version according to the git tag, and &lt;a href="https://github.com/gitpython-developers/GitPython" rel="noopener noreferrer"&gt;GitPython&lt;/a&gt; can be used to enforce that the version matches, that the repository is not dirty and has no changes on top of the tag, or anything else that &lt;em&gt;you don't want to mess up&lt;/em&gt;.  For a real world example, take a look at &lt;a href="https://github.com/intercreate/smpmgr/blob/41683521f850e39f2ce838250483699b16507f76/portable.py#L17-L28" rel="noopener noreferrer"&gt;smpmgr's build scripts&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  GitHub Release Walkthrough
&lt;/h4&gt;

&lt;p&gt;At your GitHub repository's main page, click "Releases"&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwzkdv5bos8pruiypz79t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwzkdv5bos8pruiypz79t.png" alt="GitHub Releases Link Screenshot"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click "Draft a new release"&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhvh1m29by388yxp4lkr2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhvh1m29by388yxp4lkr2.png" alt="Draft a new release screenshot"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Drop down "Choose a tag" and select a tag that you've already created or create a new one.  It should match the version in your &lt;code&gt;pyproject.toml&lt;/code&gt;!&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffryj47kbb9csb4k9kqr4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffryj47kbb9csb4k9kqr4.png" alt="choose a tag screenshot"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Use the version as the Release Title&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5l2v8x6z97r6m18r4kc4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5l2v8x6z97r6m18r4kc4.png" alt="release title screenshot"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on "Generate release notes" and then edit the release markdown with any other release information that is important to your users.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcs0rc8l9w5h7nbj6n33l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcs0rc8l9w5h7nbj6n33l.png" alt="release notes screenshot"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When you are done, click "Publish release" to create the Release page and start the Release Workflow.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frtjerf1k7w9iprdleu7v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frtjerf1k7w9iprdleu7v.png" alt="Publish release screenshot"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can view your new release page, but it won't have any assets other than a snapshot of your repository at this tag, which is default GitHub release behavior.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcsqv0jxbq1ztvgpgp42x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcsqv0jxbq1ztvgpgp42x.png" alt="release before actions complete screenshot"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To check on the progress of your Release Workflow, click on "Actions".&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuxbq31x30ksszsb6bh3i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuxbq31x30ksszsb6bh3i.png" alt="actions link screenshot"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, in the "All workflows" view, you'll see a list of actions that have succeeded (green), failed (red), or are currently running (yellow).  This screenshot shows that our recent release action is still running.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy7rsfuym758a4nqcinam.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy7rsfuym758a4nqcinam.png" alt="all workflows screenshot"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Clicking on the running workflow brings up the "Summary" where you can check in on the progress of workflows and view logs.  This is particularly useful when a workflow fails!&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqmduq61aivnvn36phjfc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqmduq61aivnvn36phjfc.png" alt="actions summary screenshot"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once the workflow has completed successfully, all artifacts will have been uploaded to the release page that only had two assets before.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9z41cg2s3eq0e92r63b3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9z41cg2s3eq0e92r63b3.png" alt="release after workflow screenshot"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Success of the workflow also means that your package has been published to PyPI.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwbb3ny4whficty694mo9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwbb3ny4whficty694mo9.png" alt="pypi screenshot"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Test the Release
&lt;/h2&gt;

&lt;p&gt;After the GitHub Release Workflow has completed, you will find the latest version of&lt;br&gt;
your package at &lt;code&gt;pypi.org/p/&amp;lt;YOUR APP&amp;gt;&lt;/code&gt;, e.g. &lt;a href="https://pypi.org/p/jpsapp/" rel="noopener noreferrer"&gt;pypi.org/p/jpsapp&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You can install it with &lt;code&gt;pip&lt;/code&gt;, but because we are focused on applications, not libraries, there is a much better tool: &lt;a href="https://pipx.pypa.io/stable/" rel="noopener noreferrer"&gt;pipx&lt;/a&gt;.  &lt;code&gt;pipx&lt;/code&gt; provides a much needed improvement to &lt;code&gt;pip&lt;/code&gt; when installing Python applications and libraries for use, rather than development.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/pypa/pipx?tab=readme-ov-file#install-pipx" rel="noopener noreferrer"&gt;pipx installation instructions&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To test your application with &lt;code&gt;pipx&lt;/code&gt;, do:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

pipx install &amp;lt;YOUR APP&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;For example, try:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

pipx install jpsapp


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;YOUR APP&amp;gt;&lt;/code&gt; will be in your system PATH and can be run from any terminal.  It&lt;br&gt;
can be upgraded to the latest version with:&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&lt;p&gt;pipx upgrade &amp;lt;YOUR APP&amp;gt;&lt;/p&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Conclusion&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;With a release workflow that is securely automated by a GitHub Action, you can quickly iterate on your application or library and provide clear instructions to your users about how to receive an authentic copy of your software.&lt;/p&gt;

&lt;p&gt;In the next part of this series, we will use the same Release Workflow to create the universally portable versions of the application so that your users do not need a Python environment to use your application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Footnotes
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a id="fn-footnotes-1"&gt;&lt;/a&gt;^ However, even if a contributor has made valuable contributions over years, you may eventually learn that you were the subject of a sophisticated social engineering campaign perpetrated by some larger government or private entity. &lt;a href="https://en.wikipedia.org/wiki/XZ_Utils_backdoor" rel="noopener noreferrer"&gt;"XZ Utils backdoor"&lt;/a&gt;. Wikipedia.com. Retrieved 2024-04-14.&lt;/li&gt;
&lt;li&gt;
&lt;a id="fn-footnotes-2"&gt;&lt;/a&gt;^ &lt;a href="https://survey.stackoverflow.co/2023/#section-most-popular-technologies-programming-scripting-and-markup-languages" rel="noopener noreferrer"&gt;"Stack Overflow Developer Survey 2023 - Programming, scripting, and markup languages"&lt;/a&gt;. stackoverflow.co. Retrieved 2024-04-14.&lt;/li&gt;
&lt;li&gt;
&lt;a id="fn-footnotes-3"&gt;&lt;/a&gt;^ &lt;a href="https://blog.pypi.org/posts/2023-11-14-1-pypi-completes-first-security-audit/" rel="noopener noreferrer"&gt;"PyPI Completes First Security Audit"&lt;/a&gt;. PyPI.org. Retrieved 2024-04-14.&lt;/li&gt;
&lt;li&gt;
&lt;a id="fn-footnotes-4"&gt;&lt;/a&gt;^ &lt;a href="https://checkmarx.com/blog/over-170k-users-affected-by-attack-using-fake-python-infrastructure/" rel="noopener noreferrer"&gt;"Over 170K Users Affected by Attack Using Fake Python Infrastructure"&lt;/a&gt;. Checkmarx.com. Retrieved 2024-04-14.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>python</category>
      <category>githubactions</category>
      <category>opensource</category>
      <category>security</category>
    </item>
    <item>
      <title>Building a Universally Portable Python App</title>
      <dc:creator>JP Hutchins</dc:creator>
      <pubDate>Sat, 16 Mar 2024 21:28:15 +0000</pubDate>
      <link>https://dev.to/jphutchins/building-a-universally-portable-python-app-2gng</link>
      <guid>https://dev.to/jphutchins/building-a-universally-portable-python-app-2gng</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F93k84tyowxjhuv2dwlj1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F93k84tyowxjhuv2dwlj1.png" alt="Python Logo"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Welcome to the first article of a series about deploying a universally portable Python application.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a "Universally Portable" app?
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;portable&lt;/strong&gt;, or &lt;strong&gt;standalone&lt;/strong&gt;, application is one that has no install-time or run-time dependencies other than the operating system.&lt;sup id="fnr-footnotes-1"&gt;1&lt;/sup&gt; It is common to see this kind of application distributed as a compressed archive, such as a .zip or .tar.gz, or as an image, like .bin or .dmg.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;universal&lt;/strong&gt; application is one that can run on all operating systems and architectures.  Here, we use "universal" loosely to mean the three personal computer operating systems that make up over 90% of global market share: &lt;strong&gt;Windows&lt;/strong&gt; (72.13%), &lt;strong&gt;MacOS&lt;/strong&gt; (15.46%), and &lt;strong&gt;Linux&lt;/strong&gt; (4.03%).&lt;sup id="fnr-footnotes-2"&gt;2&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;Windows and Linux builds will target the &lt;strong&gt;amd64&lt;/strong&gt; (x86-64) architecture and MacOS will target Arm64 (&lt;strong&gt;"Apple Silicon"&lt;/strong&gt; M-series Macs).  Arm, aarch64 or arm32, builds for Linux would be possible locally but are not available in a GitHub Workflow, &lt;a href="https://github.blog/changelog/2023-10-30-accelerate-your-ci-cd-with-arm-based-hosted-runners-in-github-actions/" rel="noopener noreferrer"&gt;yet&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You can test the output of this tutorial by installing the example application, &lt;code&gt;jpsapp&lt;/code&gt;, yourself.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use portable ZIPs or OS installers
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/JPHutchins/python-distribution-example/releases" rel="noopener noreferrer"&gt;GitHub: jpsapp releases page&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Install with pipx
&lt;/h3&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

pipx install jpsapp
jpsapp --help


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  The Series
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;This article: build the app locally with &lt;a href="https://build.pypa.io/en/stable/" rel="noopener noreferrer"&gt;build&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Use a GitHub Release Action to automate distribution to &lt;a href="https://pypi.org/" rel="noopener noreferrer"&gt;PyPI, the Python Package Index&lt;/a&gt; so that Python users can install your app with &lt;a href="https://pip.pypa.io/en/stable/" rel="noopener noreferrer"&gt;pip&lt;/a&gt; and &lt;a href="https://github.com/pypa/pipx" rel="noopener noreferrer"&gt;pipx&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Add the universal portable application build to the GitHub Release Action using &lt;a href="https://github.com/pyinstaller/pyinstaller" rel="noopener noreferrer"&gt;PyInstaller&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Add a &lt;strong&gt;Windows MSI&lt;/strong&gt; installer build to the GitHub Release Action using &lt;a href="https://wixtoolset.org/docs/intro/" rel="noopener noreferrer"&gt;WiX v4&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Add &lt;strong&gt;Linux .deb and .rpm&lt;/strong&gt; installer builds to the GitHub Release Action using &lt;a href="https://github.com/jordansissel/fpm" rel="noopener noreferrer"&gt;fpm&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Deploy to the &lt;strong&gt;Microsoft Store&lt;/strong&gt; and &lt;code&gt;winget&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Deploy to the &lt;strong&gt;Mac App Store&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Deploy to the &lt;strong&gt;Debian Archive&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  The App
&lt;/h2&gt;

&lt;p&gt;This article will focus on the application itself and the tooling to support it.&lt;/p&gt;

&lt;p&gt;The app is a command line interface (CLI) that uses the built in &lt;a href="https://docs.python.org/3/library/argparse.html" rel="noopener noreferrer"&gt;&lt;code&gt;argparse&lt;/code&gt;&lt;/a&gt; module and takes one of three actions:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;no argument: print "Hello, World!"&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-i&lt;/code&gt; or &lt;code&gt;--input&lt;/code&gt;: print "Hello, World!", then "Press any key to exit...".  This is used to create a double-clickable version of the application for Windows users&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-v&lt;/code&gt; or &lt;code&gt;--version&lt;/code&gt; argument: print package version and exit&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Take a look at the &lt;a href="https://github.com/JPHutchins/python-distribution-example/blob/main/jpsapp/main.py" rel="noopener noreferrer"&gt;source code&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  The Repo
&lt;/h2&gt;

&lt;p&gt;The repository, &lt;a href="https://github.com/JPHutchins/python-distribution-example/" rel="noopener noreferrer"&gt;python-distribution-example&lt;/a&gt;, can be cloned to your Windows, Linux, or MacOS environment.&lt;/p&gt;

&lt;p&gt;The following are excerpts and explanations of the files that are relevant to running the app locally.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: tooling and dependencies are intentionally kept to a minimum in this&lt;br&gt;
example repository.  A more complicated app that has more dependencies will&lt;br&gt;
benefit from the usage of tools that help to resolve dependencies and manage&lt;br&gt;
environments.  Unfortunately, there is no easy recommendation to make.&lt;/p&gt;

&lt;p&gt;I suggest reading Anna-Lena Popkes' article, &lt;a href="https://alpopkes.com/posts/python/packaging_tools/" rel="noopener noreferrer"&gt;An unbiased evaluation of environment management and packaging tools&lt;/a&gt;, to help you to form an opinion about what tooling is best for your application.&lt;/p&gt;

&lt;p&gt;This repository demonstrates a highly compatible &lt;code&gt;pyproject.toml&lt;/code&gt; that readers&lt;br&gt;
can easily adapt to their choice of tooling.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  &lt;code&gt;jpsapp/&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;This is the Python module itself and contains all of the source code for the application.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;__main__.py&lt;/code&gt;: support running as a module - &lt;code&gt;python -m jpsapp&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;main.py&lt;/code&gt;: the app described above
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  &lt;code&gt;envr-default&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;This file defines the shell environment for common shells like &lt;strong&gt;bash&lt;/strong&gt;, &lt;strong&gt;zsh&lt;/strong&gt;, and &lt;strong&gt;PowerShell&lt;/strong&gt; on Windows, MacOS, and Linux.  The environment is activated by calling &lt;code&gt;. ./envr.ps1&lt;/code&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ini"&gt;&lt;code&gt;

&lt;span class="nn"&gt;[PROJECT_OPTIONS]&lt;/span&gt;
&lt;span class="py"&gt;PROJECT_NAME&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;jpsapp&lt;/span&gt;
&lt;span class="py"&gt;PYTHON_VENV&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;.venv&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  &lt;code&gt;pyproject.toml&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://peps.python.org/pep-0621/" rel="noopener noreferrer"&gt;PEP 621&lt;/a&gt; introduced the &lt;code&gt;pyproject.toml&lt;/code&gt; standard for declaring common metadata, replacing the need for &lt;code&gt;requirements.txt&lt;/code&gt; and most other configuration files.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight toml"&gt;&lt;code&gt;

&lt;span class="nn"&gt;[build-system]&lt;/span&gt;
&lt;span class="py"&gt;requires&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="py"&gt;"setuptools&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;70.0&lt;/span&gt;&lt;span class="s"&gt;",&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="py"&gt;build-backend&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"setuptools.build_meta"&lt;/span&gt;

&lt;span class="nn"&gt;[project]&lt;/span&gt;
&lt;span class="py"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"jpsapp"&lt;/span&gt;
&lt;span class="py"&gt;version&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"1.1.6"&lt;/span&gt;
&lt;span class="py"&gt;description&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"An example of Python application distribution."&lt;/span&gt;
&lt;span class="py"&gt;authors&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="err"&gt;{&lt;/span&gt; &lt;span class="py"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"JP Hutchins"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="py"&gt;email&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"jphutchins@gmail.com"&lt;/span&gt; &lt;span class="err"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="py"&gt;readme&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"README.md"&lt;/span&gt;
&lt;span class="py"&gt;license&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="py"&gt;file&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"LICENSE"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="py"&gt;requires-python&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="py"&gt;"&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;3.8&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="py"&gt;classifiers&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s"&gt;"Development Status :: 4 - Beta"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;"Intended Audience :: Developers"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;"Topic :: Software Development :: Build Tools"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="py"&gt;dependencies&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="c"&gt;# Add your project dependencies here&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="nn"&gt;[project.optional-dependencies]&lt;/span&gt;
&lt;span class="py"&gt;dev&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="py"&gt;"build&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;1.2&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="s"&gt;",&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;    &lt;span class="py"&gt;"pyinstaller&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;6.4&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="s"&gt;",&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;    &lt;span class="py"&gt;"pyinstaller-versionfile&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;2.1&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="s"&gt;",&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="nn"&gt;[project.scripts]&lt;/span&gt;
&lt;span class="py"&gt;jpsapp&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"jpsapp.main:app"&lt;/span&gt;

&lt;span class="nn"&gt;[project.urls]&lt;/span&gt;
&lt;span class="py"&gt;Homepage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"https://dev.to/jphutchins/building-a-universally-portable-python-app-2gng"&lt;/span&gt;
&lt;span class="py"&gt;Repository&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"https://github.com/JPhutchins/python-distribution-example.git"&lt;/span&gt;

&lt;span class="nn"&gt;[tool.setuptools]&lt;/span&gt;
&lt;span class="py"&gt;packages&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"jpsapp"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="py"&gt;include-package-data&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;For a detailed explanation of the &lt;code&gt;pyproject.toml&lt;/code&gt;, refer to the &lt;a href="https://packaging.python.org/en/latest/guides/writing-pyproject-toml/" rel="noopener noreferrer"&gt;Python Packaging User Guide&lt;/a&gt;.  Here are a few interesting features of our example configuration.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;version = "1.1.6"&lt;/code&gt; will version the python package and the eventual app.  This&lt;br&gt;
line in the configuration is the Single Source Of Truth for the version.  There&lt;br&gt;
are many tools available to establish a Git tag as the SSOT, if you prefer.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;packages = ["jpsapp"]&lt;/code&gt; declares that &lt;code&gt;jpsapp&lt;/code&gt; is the only module we are packaging.  This allows more Python modules to be added to the root of the repository, such as the tooling in the &lt;code&gt;distrubution&lt;/code&gt; folder, that we wouldn't want to package for distribution.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;jpsapp = "jpsapp.main:app"&lt;/code&gt; declares that the command &lt;code&gt;jpsapp&lt;/code&gt; will execute the &lt;code&gt;app&lt;/code&gt; function from &lt;code&gt;jpsapp.main&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Dependencies
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Python
&lt;/h3&gt;

&lt;p&gt;If you have Python &amp;gt;=3.8 go ahead and use that.  If not, install the most recent Python release for your system.  There are many ways to do so, but I'll briefly offer my &lt;em&gt;opinion&lt;/em&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Windows: use the Microsoft Store or &lt;code&gt;winget&lt;/code&gt; and take advantage of "App Execution Aliases".  Whatever you do, make sure that both &lt;code&gt;python&lt;/code&gt; and &lt;code&gt;python3&lt;/code&gt; call the Python you want, none of this &lt;code&gt;py&lt;/code&gt; nonsense!&lt;/li&gt;
&lt;li&gt;Linux: use your package manager, and maybe &lt;a href="https://launchpad.net/~deadsnakes/+archive/ubuntu/ppa" rel="noopener noreferrer"&gt;deadsnakes&lt;/a&gt; if you're on Ubuntu since they don't keep their Python packages current.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Build the App
&lt;/h2&gt;

&lt;p&gt;Now that you have &lt;a href="https://github.com/JPHutchins/python-distribution-example" rel="noopener noreferrer"&gt;cloned the repository&lt;/a&gt; and installed the dependencies, you can build and run the application.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;python3 -m venv .venv&lt;/code&gt;: on this first run it will create the venv at &lt;code&gt;.venv&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;python3&lt;/code&gt; is not an alias to the version of Python 3 you'd like to use then
update the command accordingly, e.g. &lt;code&gt;python -m venv .venv&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;
&lt;code&gt;. ./envr.ps1&lt;/code&gt;: activate the development environment&lt;/li&gt;

&lt;li&gt;
&lt;code&gt;pip install --require-virtualenv -e .[dev]&lt;/code&gt;: install the development dependencies&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;And that's it!  &lt;code&gt;jpsapp&lt;/code&gt; should print "Hello, World!".  Keep in mind that you can get the same execution with &lt;code&gt;python -m jpsapp&lt;/code&gt;, &lt;code&gt;python -m jpsapp.main&lt;/code&gt;, or &lt;code&gt;python jpsapp/main.py&lt;/code&gt;, etc.&lt;/p&gt;

&lt;p&gt;To build the Python package distributions, simply run &lt;code&gt;python -m build&lt;/code&gt;.  The Python &lt;code&gt;.whl&lt;/code&gt; and &lt;code&gt;.tar.gz&lt;/code&gt; packages will be built at &lt;code&gt;dist/&lt;/code&gt;, e.g. &lt;code&gt;dist/jpsapp-1.0.0.tar.gz&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In the &lt;a href="https://dev.to/jphutchins/github-release-action-for-the-python-package-index-1m7n"&gt;next part of this series&lt;/a&gt;, we will use a GitHub Workflow to release the package distribution to the PyPI so that other users can install your app with &lt;code&gt;pipx&lt;/code&gt;!&lt;/p&gt;

&lt;h2&gt;
  
  
  Footnotes
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a id="fn-footnotes-1"&gt;&lt;/a&gt;^ &lt;a href="https://en.wikipedia.org/wiki/Portable_application" rel="noopener noreferrer"&gt;"Portable application"&lt;/a&gt;. Wikipedia.com. Retrieved 2024-03-11.
&lt;/li&gt;
&lt;li&gt;
&lt;a id="fn-footnotes-2"&gt;&lt;/a&gt;^ &lt;a href="https://gs.statcounter.com/os-market-share/desktop/worldwide" rel="noopener noreferrer"&gt;"OS Market Share"&lt;/a&gt;. GS.Statcounter.com. Retrieved 2024-03-11.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Change History
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;2024-04-14: change &lt;code&gt;myapp&lt;/code&gt; -&amp;gt; &lt;code&gt;yourapp&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;2024-04-18: change &lt;code&gt;yourapp&lt;/code&gt; -&amp;gt; &lt;code&gt;jpsapp&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;2024-06-08: remove poetry; update pyproject.toml; update steps&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
      <category>githubactions</category>
      <category>opensource</category>
      <category>security</category>
    </item>
    <item>
      <title>Update Python unittest with asyncio tests for aiohttp and more</title>
      <dc:creator>JP Hutchins</dc:creator>
      <pubDate>Tue, 08 Dec 2020 02:56:21 +0000</pubDate>
      <link>https://dev.to/jphutchins/update-python-unittest-with-asyncio-tests-for-aiohttp-and-more-31aj</link>
      <guid>https://dev.to/jphutchins/update-python-unittest-with-asyncio-tests-for-aiohttp-and-more-31aj</guid>
      <description>&lt;h1&gt;
  
  
  Add asynchronous IO to an older library
&lt;/h1&gt;

&lt;p&gt;Recently I was tasked with adding full &lt;code&gt;asyncio&lt;/code&gt; compatibility to an established library that was using &lt;code&gt;requests&lt;/code&gt;.  I chose to add an &lt;code&gt;aiohttp&lt;/code&gt; option in addition to &lt;code&gt;requests&lt;/code&gt; and unify the interfaces completely: the only difference for the new async interface would be adding a keyword to the class constructor and then using the &lt;code&gt;await&lt;/code&gt; keyword for all IO.  Fantastic!  Or so I thought.&lt;/p&gt;

&lt;h1&gt;
  
  
  The unit testing problem
&lt;/h1&gt;

&lt;p&gt;Like many libraries, this one has extensive unit tests in place using Python &lt;code&gt;unittest&lt;/code&gt;.  For any tests that did not directly use IO I was able to add a novel decorator discussed at the bottom of this post.  The trouble came where the library was using &lt;code&gt;mock&lt;/code&gt; with the &lt;code&gt;@mock.patch("requests.post")&lt;/code&gt; decorator to stub &lt;code&gt;requests&lt;/code&gt;.  Did I need to mock &lt;code&gt;aiohttp&lt;/code&gt; in the same way?  &lt;a href="https://docs.aiohttp.org/en/stable/testing.html#faking-request-object"&gt;The documentation discourages it.&lt;/a&gt;  After experimenting with some proposed patterns and dependencies, I have settled on what I present below as being minimally invasive and avoiding too many new imports.&lt;/p&gt;

&lt;h1&gt;
  
  
  An aiohttp server
&lt;/h1&gt;

&lt;h4&gt;
  
  
  async_server.py
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;aiohttp&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;web&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;tests.helpers&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;SimpleMock&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;SimpleMockRequest&lt;/span&gt;


&lt;span class="n"&gt;routes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;web&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;RouteTableDef&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;mock_resp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;SimpleMock&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;mock_req&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;SimpleMockRequest&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;


&lt;span class="nd"&gt;@routes.route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;*&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/{_:.*}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;mock_req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;web&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;mock_resp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;setup_web_server&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;host&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;localhost&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;8109&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;runner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;web&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;AppRunner&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;runner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setup&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;site&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;web&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;TCPSite&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;runner&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;host&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;site&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;web&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Application&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_routes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;routes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Our first import is the only new import required to update our test cases to handle aiohttp: a simple server &lt;code&gt;aiohttp.web&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Import SimpleMock and SimpleMockRequest, discussed below&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;routes = web.RouteTableDef()&lt;/code&gt; gives us a route object&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;mock_resp&lt;/code&gt; and &lt;code&gt;mock_req&lt;/code&gt; will be used as pointers to the current instances of a mocked response or request&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;handler()&lt;/code&gt; function will match all methods and paths and:

&lt;ul&gt;
&lt;li&gt;update the mocked request with the current request &lt;code&gt;mock_req.update(request)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;return the mocked response &lt;code&gt;return web.Response(**mock_resp)&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;setup_web_server()&lt;/code&gt; will add the server to the event loop&lt;/li&gt;
&lt;li&gt;The simple web app is instantiated as &lt;code&gt;app = web.Application()&lt;/code&gt; and registers the route to the handler &lt;code&gt;app.add_routes(routes)&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  The mock response, "mock" request, async decorator
&lt;/h1&gt;

&lt;h4&gt;
  
  
  helpers.py
&lt;/h4&gt;

&lt;p&gt;The mocked response was initially a plain dict, &lt;code&gt;mock_resp = {}&lt;/code&gt; but I wanted to make the interface a bit more usable.  SimpleMock is just a case insensitive dict that maps keys to attributes so that you can match the interface of mocked &lt;code&gt;request.post&lt;/code&gt; more closely.  For example, your unit test using &lt;code&gt;requests&lt;/code&gt; might look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;resp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Mock&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;resp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;MOCK_RESPONSE&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;where the new interface will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;mock_resp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;MOCK_RESPONSE&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SimpleMock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Case insensitive dict to mock HTTP response.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nf"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SimpleMock&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;()):&lt;/span&gt;
            &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SimpleMock&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;__setitem__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__setitem__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nf"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SimpleMock&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;__setitem__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__getitem__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SimpleMock&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;__getitem__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__setattr__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;__setitem__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__getattr__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;__getitem__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;SimpleMockRequest on the other hand will probably need to be tinkered with for individual needs.  I only needed method, host, url, headers, and body but added a few more entries in &lt;code&gt;attributes&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SimpleMockRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SimpleMock&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Case insensitive dict interface for an aiohttp Request object.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;clear&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;attributes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
            &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;method&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;host&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;path&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;path_qs&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;query&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;body&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
        &lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;SimpleMock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;SimpleMock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# match requests interface
&lt;/span&gt;        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;url_object&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;attr&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;attributes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;attr&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getattr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;attr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;AttributeError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;attr&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you've gathered by now, we aren't mocking the request; we are sending a real request to a real server.  The server will use the &lt;code&gt;update()&lt;/code&gt; method on the pointer &lt;code&gt;mock_req&lt;/code&gt; so that we have access to the real request that the server received shortly after it was sent.  The server then responds with the truly mocked &lt;code&gt;mock_resp&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;self.clear()&lt;/code&gt; is significant here - there is one mock_req for the whole test suite so we need to clear the old one!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We will also need this async decorator to run our unittest methods that are declared with &lt;code&gt;async def&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;functools&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;wraps&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;async_test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
    Decorator to create asyncio context for asyncio methods or functions.
    &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="nd"&gt;@wraps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;g&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run_until_complete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;args[0]&lt;/code&gt; will be "self" when a method is called.&lt;/p&gt;

&lt;h1&gt;
  
  
  Integrate with unittest
&lt;/h1&gt;

&lt;p&gt;With the helpers in place, we can import them into a test file, &lt;code&gt;test_api.py&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  test_api.py - imports
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;tests.helpers&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;async_test&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;tests.async_server&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;mock_resp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;mock_req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;setup_web_server&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  test_api.py - unit test setup and teardown
&lt;/h4&gt;

&lt;p&gt;Get event loop, start async server, init anything else for tests&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TestSomeEndpointsAndParsers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;unittest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;TestCase&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="nd"&gt;@classmethod&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;setUpClass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# you probably have some existing code above here
&lt;/span&gt;        &lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;loop&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_event_loop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;future&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run_until_complete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="nf"&gt;setup_web_server&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;host&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;LOCALHOST&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;ASYNC_SERVER_PORT&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# What you need may be different, just a quick example
# I init an "async version" of the library I'm testing
# This gives me clever trick I'll discuss at end
&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;setUp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Device&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

        &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
            &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;session&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;aiohttp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ClientSession&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;async_server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Device&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;use_async&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;async_server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;async_init&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run_until_complete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;

&lt;span class="c1"&gt;# You do need to clear the mock_resp after each test case
&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;tearDown&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;mock_resp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;clear&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  

        &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
            &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run_until_complete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Now we will use the pattern below to add async tests
&lt;/h4&gt;

&lt;p&gt;In addition to using the async keyword, make sure to prefix or postfix async to the test name itself so you do not override the synchronous one!&lt;/p&gt;

&lt;h5&gt;
  
  
  Test that an endpoint is called and the mocked response is parsed correctly:
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nd"&gt;@async_test&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_callaction_param_async&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
    Call an action with parameters and get the results.
    &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="n"&gt;mock_resp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;TEST_CALLACTION_PARAM&lt;/span&gt;
    &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;async_server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetPortMapping&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;NewPortMappingIndex&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;assertEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;NewInternalClient&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;10.0.0.1&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;assertEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;NewExternalPort&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;51773&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;assertEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;NewEnabled&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Test that an endpoint is called with a well formed request:
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nd"&gt;@async_test&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_subscribe_async&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
    Should perform a well formed HTTP SUBSCRIBE request.
    &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="n"&gt;cb_url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;http://127.0.0.1:5005&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;async_server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;async_subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cb_url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="n"&gt;UnexpectedResponse&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;pass&lt;/span&gt;
    &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;assertEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mock_req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;method&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;SUBSCRIBE&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;assertEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mock_req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;assertEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mock_req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;SCRINGLE&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;scrumple&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;assertEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mock_req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;CALLBACK&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;&amp;lt;%s&amp;gt;&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;cb_url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;assertEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mock_req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;HOST&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;ASYNC_HOST&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;assertEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mock_req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;TIMEOUT&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;123&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;I enjoyed using this pattern to add async tests to a large set of tests.  Comment below with what you've found to be the best pattern for testing a code base that maintains support for synchronous as well as asynchronous IO.&lt;/p&gt;

&lt;h3&gt;
  
  
  Bonus
&lt;/h3&gt;

&lt;p&gt;Many of the test cases I was working on didn't actually do IO.  However, since an async instance of the main class used a different constructor and an &lt;code&gt;async_init()&lt;/code&gt; that DID make IO, it seemed worthwhile to add tests for these test cases as well.  What I came up with is a decorator that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;runs the test case on the synchronous instance&lt;/li&gt;
&lt;li&gt;runs the test case on the asynchronous instance&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is not plug and play - it uses hard coded names: &lt;code&gt;self.server&lt;/code&gt; and &lt;code&gt;self.async_server&lt;/code&gt; are defined in the unittest &lt;code&gt;setUp()&lt;/code&gt; method. The other downside is that if a test case fails you won't be able to tell whether it was the synchronous or asynchronous instance that had an error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;functools&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;wraps&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;add_async_test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
    Test both the synchronous and async methods of the device (server).
    &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="nd"&gt;@wraps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;g&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nf"&gt;f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# run the original test
&lt;/span&gt;        &lt;span class="n"&gt;async_args&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;  &lt;span class="c1"&gt;# make mutable copy of args
&lt;/span&gt;        &lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;async_args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;server&lt;/span&gt;  &lt;span class="c1"&gt;# save reference to self.server
&lt;/span&gt;        &lt;span class="n"&gt;async_args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;async_args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;async_server&lt;/span&gt;  &lt;span class="c1"&gt;# set copy.server to async_server
&lt;/span&gt;        &lt;span class="nf"&gt;f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;async_args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# run the test using the async instance
&lt;/span&gt;        &lt;span class="n"&gt;async_args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt;  &lt;span class="c1"&gt;# point self.server back to original
&lt;/span&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it!  Decorate all the test cases that don't do IO with &lt;code&gt;@add_async_test&lt;/code&gt; and save a bunch of code and time - if you're lucky!&lt;/p&gt;

</description>
      <category>python</category>
      <category>testing</category>
      <category>asyncio</category>
      <category>aiohttp</category>
    </item>
  </channel>
</rss>
