最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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!

发布评论

评论列表(0)

  1. 暂无评论