python跨文件的变量

Posted by Tesla9527 on January 20, 2021

目录结构如下:

img

整体思路:在settings.py文件中定义全局变量,并提供get_value和set_value接口给外部使用

settings.py

1
2
3
4
5
6
7
8
9
10
class global_var():
    '''需要定义全局变量的放在这里,最好定义一个初始值'''
    base_url = 'https://www.baidu.com'

# 对于每个全局变量,都需要定义get_value和set_value接口
def set_base_url(base_url):
    global_var.base_url = base_url
def get_base_url():
    return global_var.base_url

test_m01.py

1
2
3
4
5
6
7
8
9
10
11
import pytest
import sys
sys.path.append('.')
sys.path.append('..')
from Config import settings


def test_01():
    print(settings.get_base_url())
    settings.set_base_url('https://www.google.com')
    print(settings.get_base_url())

test_m02.py

1
2
3
4
5
6
7
8
9
10
11
import pytest
import sys
sys.path.append('.')
sys.path.append('..')
from Config import settings


def test_02():
    print(settings.get_base_url())
    settings.set_base_url('https://www.spacex.com')
    print(settings.get_base_url())

test_m03.py

1
2
3
4
5
6
7
8
9
10
11
import pytest
import sys
sys.path.append('.')
sys.path.append('..')
from Config import settings


def test_03():
    print(settings.get_base_url())
    settings.set_base_url('https://www.tesla.com')
    print(settings.get_base_url())

测试结果:

img