AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 77179915
Accepted
OverLordGoldDragon
OverLordGoldDragon
Asked: 2023-09-26 20:13:43 +0800 CST2023-09-26 20:13:43 +0800 CST 2023-09-26 20:13:43 +0800 CST

使用文件范围变量进行单元测试,无需类

  • 772

假设,无论出于何种原因,我拒绝使用matlab.unittest.TestCase. 我试图重现以下 Python 功能:

# `utils.py` is in same directory as the test file
# `test_wide_variable` gets used inside or outside of test functions
from utils import test_wide_variable

# gets used inside of test functions
file_wide_variable = [1, 2, 3]

def test_func1():
    y = (file_wide_variable, test_wide_variable)
    # ...

# ...

test_func1()
# test_func2()
test_func3()
# ...

试图

定义reset_test_params.m, 与

global P
P = struct;

然后,测试文件看起来像

reset_test_params

P.file_wide_variable = [1, 2, 3];
P.test_wide_variable = load('test_wide_variables.m').test_wide_variable;

test_func1()
% test_func2()
test_func3()
% ...

function test_func1(~)
    global P
    y = {P.file_wide_variable, P.test_wide_variable};
    % ...

% ...

问题

这种方法有什么明显的缺点吗?特别是并发文件执行?使用Python,我们可以在同一个全局命名空间(AFAIK)中同时运行多个测试,因此全局变量是一个禁忌。

matlab
  • 1 1 个回答
  • 47 Views

1 个回答

  • Voted
  1. Best Answer
    Cris Luengo
    2023-09-26T22:39:21+08:002023-09-26T22:39:21+08:00

    你的 Python 脚本,我们称之为my_tests.py,

    from utils import test_wide_variable
    
    # gets used inside of test functions
    file_wide_variable = [1, 2, 3]
    
    def test_func():
        y = (file_wide_variable, test_wide_variable)
        # ...
    

    大概被测试工具调用

    from inspect import getmembers, isfunction
    
    import my_tests
    for name, func in getmembers(my_tests, isfunction):
       func()
    

    或类似的东西(我什至没有测试过这段代码)。

    您的 MATLAB 脚本,我们称之为my_tests.m,

    reset_test_params
    
    P.file_wide_variable = [1, 2, 3];
    P.test_wide_variable = load('test_wide_variables.m').test_wide_variable;
    
    function test_func(~)
        global P
        y = {P.file_wide_variable, P.test_wide_variable};
        # ...
    

    当在测试工具中调用时,

    my_tests
    

    或同等地

    run("my_tests")
    

    将导致P在测试工具的工作区中定义(希望不会覆盖任何局部变量!)。test_func()然而,丢失了。脚本本身不调用它,因此它保持未使用和未定义状态。作为本地函数,它只能从内部调用my_tests.m。

    因此,尝试直接复制 Python 的测试脚本格式是行不通的,因为这两种语言在这些方面的工作方式根本不同。


    另请注意,当您从另一个脚本或函数(在 MATLAB 中)调用脚本时,其所有变量都将在调用者的工作区中定义。这意味着脚本可能会覆盖调用者的任何变量。使用脚本构建更复杂的软件是一个非常糟糕的主意,试图将所有脚本的变量分开将是一场噩梦。这正是函数的用途。函数具有局部作用域,使组合变得更加容易。

    除了(也许)程序的入口点之外,我不会将脚本用于任何其他用途。其他一切都应该是一个函数。没有理由不使用函数。


    这就是我尝试在 MATLAB 中重现 Python 格式的方法:

    test_wide_variable.m:

    function v = test_wide_variable
    v = [5, 6, 7];
    

    my_tests.m:

    function funcs = my_tests
       file_wide_variable = [1, 2, 3];
       data = test_wide_variable
    
       funcs = {@test_func};  % list all the test functions defined in the file
    
       function test_func
          y = {file_wide_variable, data};
          % ...
       end
    
    end
    

    现在可以从测试工具中调用它,如下所示:

    fail = 0
    total = 0
    for func = my_tests
       total = total + 1;
       try
          func()
       catch ME
          disp(ME)
          fail = fail + 1;
       end
    end
    disp(fail + "tests failed out of " + total " tests.")
    

    请注意,函数中本地声明的变量my_tests()可用于此函数中定义的嵌套函数(请注意,这些函数是在function和end语句之间声明的my_tests(),因此它们是嵌套函数,而不是本地函数。

    该my_tests()函数返回一个元胞数组,其中包含嵌套函数的句柄。然后可以通过调用测试工具来调用它们。my_tests调用时,它们仍然可以访问首次获取函数句柄时定义的变量——这些是 lambda 捕获。

    • 1

相关问题

  • 在 MATLAB 2022 中使用 ode45 时出现未定义的结果 (NaN)

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    使用 <font color="#xxx"> 突出显示 html 中的代码

    • 2 个回答
  • Marko Smith

    为什么在传递 {} 时重载解析更喜欢 std::nullptr_t 而不是类?

    • 1 个回答
  • Marko Smith

    您可以使用花括号初始化列表作为(默认)模板参数吗?

    • 2 个回答
  • Marko Smith

    为什么列表推导式在内部创建一个函数?

    • 1 个回答
  • Marko Smith

    我正在尝试仅使用海龟随机和数学模块来制作吃豆人游戏

    • 1 个回答
  • Marko Smith

    java.lang.NoSuchMethodError: 'void org.openqa.selenium.remote.http.ClientConfig.<init>(java.net.URI, java.time.Duration, java.time.Duratio

    • 3 个回答
  • Marko Smith

    为什么 'char -> int' 是提升,而 'char -> Short' 是转换(但不是提升)?

    • 4 个回答
  • Marko Smith

    为什么库中不调用全局变量的构造函数?

    • 1 个回答
  • Marko Smith

    std::common_reference_with 在元组上的行为不一致。哪个是对的?

    • 1 个回答
  • Marko Smith

    C++17 中 std::byte 只能按位运算?

    • 1 个回答
  • Martin Hope
    fbrereto 为什么在传递 {} 时重载解析更喜欢 std::nullptr_t 而不是类? 2023-12-21 00:31:04 +0800 CST
  • Martin Hope
    比尔盖子 您可以使用花括号初始化列表作为(默认)模板参数吗? 2023-12-17 10:02:06 +0800 CST
  • Martin Hope
    Amir reza Riahi 为什么列表推导式在内部创建一个函数? 2023-11-16 20:53:19 +0800 CST
  • Martin Hope
    Michael A fmt 格式 %H:%M:%S 不带小数 2023-11-11 01:13:05 +0800 CST
  • Martin Hope
    God I Hate Python C++20 的 std::views::filter 未正确过滤视图 2023-08-27 18:40:35 +0800 CST
  • Martin Hope
    LiDa Cute 为什么 'char -> int' 是提升,而 'char -> Short' 是转换(但不是提升)? 2023-08-24 20:46:59 +0800 CST
  • Martin Hope
    jabaa 为什么库中不调用全局变量的构造函数? 2023-08-18 07:15:20 +0800 CST
  • Martin Hope
    Panagiotis Syskakis std::common_reference_with 在元组上的行为不一致。哪个是对的? 2023-08-17 21:24:06 +0800 CST
  • Martin Hope
    Alex Guteniev 为什么编译器在这里错过矢量化? 2023-08-17 18:58:07 +0800 CST
  • Martin Hope
    wimalopaan C++17 中 std::byte 只能按位运算? 2023-08-17 17:13:58 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve