简体   繁体   English

如何用内部状态测试oracle包?

[英]How to test oracle packages with inner state?

Suppose we have an oracle package, which was initialized with some value grabbed from a table holding global parameters. 假设我们有一个oracle程序包,它是使用保存有全局参数的表中的某些值初始化的。 Despite the fact that it is an ugly practice, it could work in production since the parameters are to be set during installation and not likely to change frequently. 尽管这是很丑陋的做法,但由于可以在安装过程中设置参数并且不太可能经常更改,因此它可以在生产中使用。 But when one is testing the package, it's needed to reinitialize its state before each case. 但是,当测试包时,需要在每种情况之前重新初始化其状态。

I tried two approaches: 我尝试了两种方法:

  1. execute immediate 'ALTER PACKAGE my_package COMPILE'; 立即执行“ ALTER PACKAGE my_package COMPILE”;
  2. dbms_session.reset_package; dbms_session.reset_package;

The first doesn't work in some rare cases (i don't know why). 第一种在极少数情况下不起作用(我不知道为什么)。

The second resets all the packages touched by current session, including a package, which contains test cases, so any call of reset_package leads to abort execution of tests. 第二个命令重置当前会话涉及的所有程序包,包括一个包含测试用例的程序包,因此对reset_package的任何调用都会导致测试的执行中止。

What do I need to correctly test a package with inner state, when there's a need to change the state during tests execution? 当在测试执行期间需要更改状态时,我需要正确地测试具有内部状态的软件包吗?

There are at least two ways to do this. 至少有两种方法可以做到这一点。 First of all, nobody prohibits you to publish an 'Initialize' subroutine and use it for re-reading variables before each test. 首先,没有人禁止您发布“初始化”子例程并将其用于在每次测试之前重新读取变量。 It's a better way, I guess, but if you wish, you can add to this package something like 我想这是一个更好的方法,但是如果您愿意,可以在此包中添加以下内容:

procedure MyPublicMethod is
begin
  if UnitTesting.RightNow then Initialize; end if;
  ...
end;

and got the same effect automatically. 并自动获得相同的效果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM