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

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

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!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745592705a4634926.html

相关推荐

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

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

    10小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信