Python:从另一个模块访问变量仅作为引用提供。

huangapple go评论68阅读模式
英文:

Python : access to a variable from an other module is only given as a reference

问题

I understand your request to only provide translations without additional information. Here's the translation of your provided text:

我正在编写一个项目的测试,这个项目存在严重的技术债务,我遇到了我理解python变量引用方式的限制。

以下是一个非常简化的示例:

main.py
src/
init.py
foo.py
variables.py
tests/
init.py
unit_test.py
main.py

__init__.pymain.py 文件是空的。

foo.py

from src.variables import GLOBAL_VARIABLE

class Foo:
def bar(self):
print(f"foo :{id(GLOBAL_VARIABLE)}")
if GLOBAL_VARIABLE:
return 1
return 2

variables.py

GLOBAL_VARIABLE = False
print(f"\norig:{id(GLOBAL_VARIABLE)}")

unit_test.py

import src.variables
from src.foo import Foo

def testFoo():
src.variables.GLOBAL_VARIABLE = True
print(f"\ntest:{id(src.variables.GLOBAL_VARIABLE)}")
foo = Foo()
assert foo.bar() == 1

我试图更改包含在 src/variables.py 文件中的变量的值,以便运行一些测试,但我只能获得一个副本,而不是变量本身。

输出如下(只是运行 pytest,没有配置或参数):

orig:140734094465928
collected 1 item

tests\unit_test.py
test:140734094465896
foo :140734094465928

我似乎无法从测试模块访问变量本身,只能获得一个副本。我尝试了不同的测试导入方式,但我始终得到一个具有不同 id 的变量,简单失败了。

我如何更改 GLOBAL_VARIABLE 的值?

我尝试了一些解决方案,比如这个,但无法更改我的布尔值(不同之处似乎在于我从另一个模块导入?)

作为一些背景信息,我不能更改被测试的代码(想要这样做),只能更改测试,因此无法在被测试的代码上实现更好的编码实践。

英文:

I am working on writing the tests for a project that has a massive technical debt and am running into limitations of my understanding as to how python references variables

Here is the very simplified example :

main.py
src/
  __init__.py
  foo.py
  variables.py
tests/
  __init__.py
  unit_test.py
main.py

the __init__.py and main.py files are empty

foo.py

from src.variables import GLOBAL_VARIABLE


class Foo:
    def bar(self):
        print(f"foo :{id(GLOBAL_VARIABLE)}")
        if GLOBAL_VARIABLE:
            return 1
        return 2

variables.py

GLOBAL_VARIABLE = False
print(f"\norig:{id(GLOBAL_VARIABLE)}")

unit_test.py

import src.variables
from src.foo import Foo


def testFoo():
    src.variables.GLOBAL_VARIABLE = True
    print(f"\ntest:{id(src.variables.GLOBAL_VARIABLE)}")
    foo = Foo()
    assert foo.bar() == 1

I am attempting to change the value of a variable contained in the src/variables.py file in order to run some tests but I can only get a copy rather than the variable itself

The output is as follows ( simply running pytest with no configuration nor arguments ) :

orig:140734094465928
collected 1 item                                                                                                                                                                                                                                                                                            

tests\unit_test.py
test:140734094465896
foo :140734094465928

I cannot seem to get access to the variable itself from the test module, only a copy. I have attempted different imports for the tests but I am consistently getting a variable that does not have the same id and the simply fails

How can I change the value of GLOBAL_VARIABLE ?

There are solutions that I have attempted such as <a href="https://stackoverflow.com/questions/34913078/importing-and-changing-variables-from-another-file">this one</a> but was not able to change the value of my boolean ( the difference seems to be that I am importing from an other module ? )

As a bit of context I cannot change the tested code ( would like to ), only the tests, so I cannot implement better coding practices on the tested code

答案1

得分: 1

以下是翻译好的部分:

模块 unit_test.py 实际上修改了 variables.GLOBAL_VARIABLE,但是 foo.py 中有它的一份拷贝。

你可以选择更新 unit_test.py,以便它从 foo.py 导入这个拷贝:

# unit_test.py
import src.foo
def testFoo():
    src.foo.GLOBAL_VARIABLE = True
    print(f"\ntest:{id(src.foo.GLOBAL_VARIABLE)}")
    f = src.foo.Foo()
    assert f.bar() == 1

或者你可以更新 foo.py,让它也导入 variables 而不是 from variables import GLOBAL_VARIABLE

# foo.py
import src.variables
class Foo:
    def bar(self):
        print(f"foo:{id(src.variables.GLOBAL_VARIABLE)}")
        if src.variables.GLOBAL_VARIABLE:
            return 1
        return 2

我并不是在说在生产代码中导入并修改全局变量是个好主意,但是在测试中,这可能是可以接受的。

英文:

The module unit_test.py actually modifies the variables.GLOBAL_VARIABLE, but the foo.py has a copy of it.

You could either update the unit_test.py so that it imports the copy from foo.py:

# unit_test.py
import src.foo
def testFoo():
    src.foo.GLOBAL_VARIABLE = True
    print(f&quot;\ntest:{id(src.foo.GLOBAL_VARIABLE)}&quot;)
    f = src.foo.Foo()
    assert f.bar() == 1

Or you could update the foo.py so that it also does import variables instead of from variables import GLOBAL_VARIABLE.

# foo.py
import src.variables
class Foo:
    def bar(self):
        print(f&quot;foo:{id(src.variables.GLOBAL_VARIABLE)}&quot;)
        if src.variables.GLOBAL_VARIABLE:
            return 1
        return 2

I am not saying that importing and changing global variables from other modules is a good idea for production codes, but for testing, this is probably fine.

huangapple
  • 本文由 发表于 2023年4月19日 23:32:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76056354.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定