最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

pytest - How to mock a python-jenkins call in FastAPI - Stack Overflow

matteradmin1PV0评论

I have a simple FastAPI application that uses python-jenkins to make custom calls to our Jenkins instance.

Here are a couple of examples:

@app.get("/jenkins_data") 
async def get_jenkins_nodes() -> list:
    server = jenkins.Jenkins(url, user, key)

    try:
        nodes = server.get_nodes()
        return get_nodes_and_states(nodes)
    except jenkins.JenkinsException:
        nodes = []
        return nodes

Or I might have something like this:

@app.get("/job/{node_name}")
def get_current_jenkins_job(node_name: str) -> str:    
    server = jenkins.Jenkins(url, user, key)
    node = server.get_node_info(f"{node_name}", 2)
    if node["executors"][0]["currentExecutable"] is not None:
        display_name = node["executors"][0]["currentExecutable"]["displayName"]
    else:
        display_name = "No Jobs Running"
    return display_name

How do I go about mocking the Jenkin's object so I can properly test these methods with pytest? I want to write a test that causes Jenkins to raise jenkins.JenkinsException in the first example or mock it to set the "currentExecutable" for the second.

Thanks!

Post a comment

comment list (0)

  1. No comments so far