【问题标题】:How to run tests in a django view without affecting the database如何在 django 视图中运行测试而不影响数据库
【发布时间】:2019-11-16 04:39:28
【问题描述】:

我正在尝试使用management.call_command 直接从django 视图运行功能测试(在python/django 中使用Selenium),以便允许用户从网站运行测试。 django 视图类似于:

class MyView():
    def get(self):
        output = call_command('test', 'folder.tests.MyTest')
        # doing ./manage.py test folder.tests.MyTest
        test_result = 'Test result: ' + output
        return something_http_with_test_result

为了不影响当前用户数据,最好的方法是什么? MyTest 将在数据库中创建大量对象,但用户不能看到它们。

谢谢

【问题讨论】:

    标签: python django testing command


    【解决方案1】:

    我发现正确运行它的最佳方法是使用 os.system :

    import os
    dir = 'your_absolute_path_to_project'
    class MyView:
        def some_func_called():
            os.system(dir + '/manage.py test >' + dir + '/log.txt')
    

    项目和 firefox 必须拥有相同的所有者,结果将在 log.txt 中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-02
      • 1970-01-01
      • 1970-01-01
      • 2011-03-06
      相关资源
      最近更新 更多