小编use*_*923的帖子

在 pytest 参数化中使用 self

如何parametrize在类中组织的测试中使用 pytest 中的属性?

import pytest

class TestA:
    @pytest.fixture(autouse=True)
    def set_up(self):
        self.field1 = "field1"
        self.field2 = "field2"

    @pytest.mark.parametrize("field", (self.field1, self.field2))
    def test_print_field(self, field):
        print(field, flush=False)
Run Code Online (Sandbox Code Playgroud)

我越来越NameError: name 'self' is not defined

python pytest

5
推荐指数
1
解决办法
2802
查看次数

不允许使用 django_db 标记进行 Pytest django 数据库访问

我正在使用 pytest,并且在下面的固定装置中遇到数据库访问问题。我到处都有 django_db 标记。

[test_helpers.py]
import pytest
from django.test import Client
from weblab.middleware.localusermiddleware import _set_current_user

@pytest.fixture(scope="class")
@pytest.mark.django_db
def class_test_set_up(request):
    request.cls.client = Client()
    username = "username"
    user = User.objects.get(username=username)
    _set_current_user(user)
Run Code Online (Sandbox Code Playgroud)

我正在 RuntimeError: Database access not allowed, use the "django_db" mark, or the "db" or "transactional_db" fixtures to enable it. 排队user = User.objects.get(username=username)

[test_tmp_fixture.py]
import pytest
from tests.factories.sample.test_factories import TestFactory
from tests.tests_helpers.test_helpers import class_test_set_up

SIZE = 5

@pytest.mark.django_db
@pytest.fixture(scope="class")
def set_up_objs(request):
    request.cls.factory = TestFactory
    request.instance.objs = request.cls.factory.create_batch(SIZE)

@pytest.mark.django_db
@pytest.mark.usefixtures("class_test_set_up", "set_up_objs")
class …
Run Code Online (Sandbox Code Playgroud)

python django fixtures pytest

5
推荐指数
1
解决办法
4395
查看次数

venv 中没有激活

我的虚拟环境的 bin 文件夹中没有激活脚本。仅有的文件是:python、python3、python3.9。

如何修复我的环境而不删除它?我使用的是 venv 而不是 virtualenv。我正在使用Linux。

我的问题与此类似 - 当我尝试运行虚拟环境时没有激活

其中一个答案说我应该跑步。

python3.7 -m venv venv

但我不明白这个语法。我应该在终端中写入 python3.9 -m venv venv 吗?它会改善我的环境吗?

我还想说,新创建的环境按预期工作,所有其他环境也按预期工作。

python python-venv

3
推荐指数
1
解决办法
5404
查看次数

条件评估是否优化?这段代码不好吗?

1.Imagine条件if (obj.is_x() || obj.is_y() || obj.is_z())
obj.is_y()obj.is_z()被调用,如果评估obj.is_x()返回真的吗?

这个坏主意(一般来说)?这段代码看起来不好吗?

bool isbn13_prefix_valid (const string& prefix)
{
    unsigned num = stoi(prefix);
    if (num == 978 || num == 979) return 1;  //super common ones
        else if (   num >= 0 && num <= 5 || num == 7 || num >= 600 && num <= 649
                || num >= 80 && num <= 94 || num >= 950 && num <= 989
                || num >= 9900 …
Run Code Online (Sandbox Code Playgroud)

c++ optimization evaluation logical-operators conditional-statements

2
推荐指数
1
解决办法
108
查看次数

在括号中定义变量

我不明白为什么变量i在一开始就不为人所知.如何在括号中定义变量?(如果使用int i我仍然会收到错误)

void cardlike(vector<int> &v)
{
        unsigned max_pos = 0;
        int tmp;
        for (unsigned i = 0; i < v.size(); i++);
        {
                for (unsigned j = 0; j < v.size() - i; j++)
                        if(v[j] > v[max_pos])
                                max_pos = j;

                tmp = v[max_pos];
                v[max_pos] = v[v.size() - i - 1];
                v[v.size() - i - 1] = tmp; 
        }
}
Run Code Online (Sandbox Code Playgroud)

当我用-std = c ++ 11编译时,这就是我从g ++中得到的:

sortvector.cpp:93:38: error: ‘i’ was not declared in this scope   
   for(unsigned j = 0; j < v.size() …
Run Code Online (Sandbox Code Playgroud)

c++ scope for-loop declaration

1
推荐指数
1
解决办法
82
查看次数