使用python-behave时内存使用率很高

时间:2015-09-23 11:09:24

标签: python-behave

使用python-behave时遇到了问题。

我在每个场景之前创建一个沙箱数据库,然后在场景之后将其销毁。

但每种情况下内存使用量增加约20MB,所有测试用例(LoL)的总使用量约为3.xGB。

我的问题是当我调用context.runner.teardown_databases()时,为什么不释放内存?

        from django.test.runner import DiscoverRunner
        def before_scenario(context, scenario):
            context.runner = DiscoverRunner()
            context.runner.setup_test_environment()
            context.old_db_config = context.runner.setup_databases()

        def after_scenario(context, scenario):
            context.runner.teardown_databases(context.old_db_config)
            context.runner.teardown_test_environment()

    Feature:
      Scenario: 
        Given I have a debit card
        When I withdraw 200 
        Then I should get $200 in cash
    @given('I have a debit card')
    def step1(context):
        pass
    @given('I withdraw 200')
    def step2(context):
        pass
    @given('I should get $200 in cash')
    def step3(context):
        pass

python-behave:版本1.2.5

django:版本1.8.0

python:版本2.7

任何建议都表示赞赏。

1 个答案:

答案 0 :(得分:0)

添加gc.garbage()后,我找到了根本原因。

def after_scenario(context, scenario):
    context.runner.teardown_databases(context.old_db_config)
    context.runner.teardown_test_environment()
    gc.collect()
    print gc.garbage

那些无法收集的对象应该是模型实例,因为这些实例是循环引用的。 然后我添加一个清除函数来删除thmem,现在内存正常。

相关问题