5 Easy Steps To Get Your Take a look at Suite Working in Heroku CI – DZone – Uplaza

So, I’ve at all times thought of Heroku as only a place to run my code. They’ve a CLI. I can join it to my GitHub repo, push my code to a Heroku distant, and bam…it’s deployed. No fuss. No mess.

However I had at all times run my check suite…some other place: domestically, or with CircleCI, or in GitHub Actions. How did I not know that Heroku has CI capabilities? Do you imply I can run my assessments there? The place have I been for the previous few years?

In order that’s why I didn’t find out about Heroku CI…

CI is fairly superior. You’ll be able to construct, check, and combine new code modifications. You get quick suggestions on these code modifications as a way to determine and repair points early. In the end, you ship higher-quality software program.

By doing it in Heroku, I get my check suite working in an atmosphere a lot nearer to my staging and manufacturing deployments. If I piece collectively a pipeline, I can automate the development from passing assessments to a staging deployment after which promote that staged construct to manufacturing.

So, how will we get our software check suite up and working in Heroku CI? It would take you 5 steps:

  1. Write your assessments
  2. Deploy your Heroku app
  3. Push your code to Heroku
  4. Create a Heroku Pipeline to make use of Heroku CI
  5. Run your assessments with Heroku CI

We’ll stroll by way of these steps by testing a easy Python software. If you wish to observe alongside, you possibly can clone my GitHub repo.

Our Python App: Is It Prime?

We’ve constructed an API in Python that listens for GET requests on a single endpoint:/prime/{quantity}. It expects a quantity as a path parameter after which returns true or false primarily based on whether or not that quantity is a main quantity. Fairly easy.

We now have a modularized perform in is_prime.py:

def is_prime(num):
    if num 

Then, our predominant.py file seems to be like this:

from fastapi import FastAPI, HTTPException
from is_prime import is_prime

app = FastAPI()

# Path to verify if a quantity is a main quantity
@app.get("/prime/{number}")
def check_if_prime(quantity: int):
    return is_prime(quantity)
    elevate HTTPException(status_code=400, element="Input invalid")

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="localhost", port=8000)

That’s all there may be to it. We are able to begin our API domestically (python predominant.py) and ship some requests to strive it out:

~$ curl http://localhost:8000/prime/91
false

~$ curl http://localhost:8000/prime/97
true

That appears fairly good. However we’d really feel higher with a unit check for the is_prime perform. Let’s get to it.

Step #1: Write Your Assessments

With pytest added to our Python dependencies, we’ll write a file referred to as test_is_prime.py and put it in a subfolder referred to as assessments. We now have a set of numbers that we’ll check to ensure our perform determines accurately if they’re prime or not. Right here’s our check file:

from is_prime import is_prime

def test_1_is_not_prime():
    assert not is_prime(1)

def test_2_is_prime():
    assert is_prime(2)

def test_3_is_prime():
    assert is_prime(3)

def test_4_is_not_prime():
    assert not is_prime(4)

def test_5_is_prime():
    assert is_prime(5)

def test_991_is_prime():
    assert is_prime(991)

def test_993_is_not_prime():
    assert not is_prime(993)

def test_7873_is_prime():
    assert is_prime(7873)

def test_7802143_is_not_prime():
    assert not is_prime(7802143)

Once we run pytest from the command line, right here’s what we see:

~/undertaking$ pytest
=========================== check session begins ===========================
platform linux -- Python 3.8.10, pytest-8.0.2, pluggy-1.4.0
rootdir: /dwelling/michael/undertaking/assessments
plugins: anyio-4.3.0
collected 9 objects                                                                                                                                                                                            

test_is_prime.py .........                                                                                                                                                                             [100%]

============================ 9 handed in 0.02s ============================

Our assessments move! It seems to be like is_prime is doing what it’s alleged to.

Step #2: Deploy Your Heroku App

It’s time to wire up Heroku. Assuming you’ve got a Heroku account and also you’ve put in the CLI, creating your app goes to go fairly shortly.

Heroku will look in our undertaking root folder for a file referred to as necessities.txt, itemizing the Python dependencies our undertaking has. That is what the file ought to seem like:

fastapi==0.110.1
pydantic==2.7.0
uvicorn==0.29.0
pytest==8.0.2

Subsequent, Heroku will search for a file referred to as Procfile to find out easy methods to begin our Python software. Procfile ought to seem like this:

internet: uvicorn predominant:app --host=0.0.0.0 --port=${PORT}

With these information in place, let’s create our app.

~/undertaking$ heroku login

~/undertaking$ heroku apps:create is-it-prime

That is it.

Step #3: Push Your Code to Heroku

Subsequent, we push our undertaking code to the git distant that the Heroku CLI arrange after we created our app.

~/undertaking$ git push heroku predominant
…
distant: -----> Launching...
distant:        Launched v3
distant:        https://is-it-prime-2f2e4fe7adc1.herokuapp.com/ deployed to Heroku

So, that’s performed. Let’s verify our API.

$ curl https://is-it-prime-2f2e4fe7adc1.herokuapp.com/prime/91
false

$ curl https://is-it-prime-2f2e4fe7adc1.herokuapp.com/prime/7873
true

$ curl https://is-it-prime-2f2e4fe7adc1.herokuapp.com/prime/7802143
false

It really works!

Step #4: Create a Heroku Pipeline To Use Heroku CI

Now, we need to create a Heroku Pipeline with CI enabled in order that we are able to run our assessments.

We create the pipeline (referred to as is-it-prime-pipeline), including the app we created above to the staging part of the pipeline.

$ heroku pipelines:create 
  --app=is-it-prime 
  --stage=staging 
  is-it-prime-pipeline

Creating is-it-prime-pipeline pipeline... performed
Including ⬢ is-it-prime to is-it-prime-pipeline pipeline as staging... performed

With our pipeline created, we need to join it to a GitHub repo in order that our actions on the repo (reminiscent of new pull requests or merges) can set off occasions in our pipeline (like mechanically working the check suite).

$ heroku pipelines:join is-it-prime-pipeline -r capnMB/heroku-ci-demo

Linking to repo... performed

As you possibly can see, I’m connecting my pipeline to my GitHub repo. When one thing like a pull request or a merge happens in my repo, it should set off the Heroku CI to run the check suite.

Subsequent, we have to configure our check atmosphere in an app.json manifest. Our file contents ought to seem like this:

{
  "environments": {
    "test": {
      "formation": {
        "test": {
          "quantity": 1,
          "size": "standard-1x"
        }
      },
      "scripts": {
        "test": "pytest"
      }
    }
  }
}

This manifest accommodates the script we’d use to run by way of our check suite. It additionally specifies the dyno measurement we (standard-1x) would need to use for our check atmosphere. We commit this file to our repo.

Lastly, within the internet UI for Heroku, we navigate to the Assessments web page of our pipeline, and we click on the Allow Heroku CI button.

After enabling Heroku CI, right here’s what we see:

Step #5: Run Your Assessments With Heroku CI

Simply to reveal it, we are able to manually set off a run of our check suite utilizing the CLI:

$ heroku ci:run --pipeline is-it-prime-pipeline
…
-----> Working check command `pytest`...
========================= check session begins ============================
platform linux -- Python 3.12.3, pytest-8.0.2, pluggy-1.4.0
rootdir: /app
plugins: anyio-4.3.0
collected 9 objects

assessments/test_is_prime.py .........                                         [100%]

============================ 9 handed in 0.03s ============================

How does the check run look in our browser? We navigate to our pipeline and click on Assessments. There, we see our first check run within the left-side nav.

A more in-depth inspection of our assessments reveals this:

Superior. Now, let’s push some new code to a department in our repo and watch the assessments run!

We create a brand new department (referred to as new-test), including one other check case to test_is_prime.py. As quickly as we push our department to GitHub, right here’s what we see:

Heroku CI detects the pushed code and automates a brand new run of the check suite. Not too lengthy after, we see the profitable outcomes:

Heroku CI for the Win

In the event you’re already utilizing Heroku on your manufacturing atmosphere — and also you’re able to go all in with DevOps — then utilizing pipelines and Heroku CI will be the strategy to go.

Reasonably than utilizing completely different instruments and platforms for constructing, testing, reviewing, staging, and releasing to manufacturing… I can consolidate all these items in a single Heroku Pipeline and get automated testing with each push to my repo.

Share This Article
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Exit mobile version