GithubHelp home page GithubHelp logo

Testings Tasks about django-background-tasks HOT 4 CLOSED

arteria avatar arteria commented on June 15, 2024
Testings Tasks

from django-background-tasks.

Comments (4)

jvamvas avatar jvamvas commented on June 15, 2024

Here's an example:

from django.test import TestCase

from background_task import background
from background_task.tasks import tasks


completed_tasks = []

@background()
def my_background_task(message):
    completed_tasks.append(message)


class MyBackgroundTaskTestCase(TestCase):
    def test_run_task(self):
        my_background_task(message="test")
        tasks.run_next_task()
        self.assertTrue("test" in completed_tasks)

The management command process_tasks might be a helpful reference: https://github.com/arteria/django-background-tasks/blob/master/background_task/management/commands/process_tasks.py#L85

from django-background-tasks.

gregpinero avatar gregpinero commented on June 15, 2024

Thanks. That would work. But let's say I have a function used all over the code like send_email. And I decorate it to now be a background task.

Would I need to go into every single test and tell it to run_next_task(). Would there be a way to do this automatically everywhere?

from django-background-tasks.

Zagrebelin avatar Zagrebelin commented on June 15, 2024

You can use .task_function field of your task:

@background
def add(x,y):
    return x+y

add(1,2)  # will create a task
add.task_function(1,2)   # will launch your function immediatly and synchronously
add = add.task_function  # permanently rid out of background stuff for testing purpose

Also check out this PR lilspikey#17 (comment). I accidentally pushed it to wrong repo.

from django-background-tasks.

philippeowagner avatar philippeowagner commented on June 15, 2024

@Zagrebelin could you please send the PR#17 to this repo as well? Thanks

from django-background-tasks.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.