[Python] Pytest fixture saved into a pickle

Discussão em 'Python' iniciado por Stack, Julho 11, 2025.

  1. Stack

    Stack Membro Participativo

    I am working on a project where I am building on top of a solution of a very time-consuming problem. Instead of solving the very time consuming part many times, I only run the solver once, I save the solution with pickle, and then I reuse the pickle to test my features build on top of the solution.

    Here is a model of what I mean

    import time
    import pickle


    class Solution:
    """ A class that represents a solution of a very time-consuming problem. """


    def solve_difficult_problem() -> Solution:
    time.sleep(1)
    return Solution()


    def prepare_solution() -> None:
    """ I solve the problem only once. I use the solution for all the
    development of my new feature as it never changes. """
    solution = solve_difficult_problem()
    with open('solved.pickle', 'wb') as file:
    pickle.dump(solution, file)


    def my_feature(solution: Solution) -> None:
    """ I am using the solution. """
    print(solution)


    def test_my_feature() -> None:
    # prepare_solution() # I run it only once: at the first run.
    with open('solved.pickle', 'rb') as file:
    solution = pickle.load(file)
    assert my_feature(solution) is None


    My problem is that with pytest, I need to manually uncomment the prepare_solution() at the first run of my tests. Is there a way for pytest to automate this?

    I have tried writing a separate file which prepares the needed solution. But I don't know how to tell pytest that it should run it manually only the first time.

    I would like the solution to be saved as a local file that persists longer than one session. Basically, I would like the solution to be available until I remove the whole project from my computer.

    Continue reading...

Compartilhe esta Página