有没有一种方法可以访问私有的plsql过程以进行测试? [英] Is there a way to access private plsql procedures for testing purposes?
问题描述
我正在开发一个包含很多plsql代码的项目,并且想在我们的代码库中添加更多特定的单元测试.我想测试的某些过程/功能不在包装规格中,因此我无法更改.
I'm working on a project with a lot of plsql code and would like to add more specific unit-tests to our codebase. Some of the procedures/functions I like to test aren't in the package spec and I have no means to change that.
是否可以在不将其添加到规范的情况下访问这些私有" plsql过程?
Is there a way to access these 'private' plsql procedures without adding them to the spec?
到目前为止,我唯一的想法是在测试之前将特殊的程序包规范编译到DB,该规范指定了要测试的过程.我想这行得通,但是我想知道是否有更简单的方法,也许是一些邪恶的秘密甲骨文黑客;-)
The only Idea I had so far, was to compile a special package spec to the DB before the tests, that specifies the procedures under test. I gues that would work, but I wonder if there is a simpler way, some evil secret oracle hack maybe ;-)
我正在使用JUnit/DBUnit从Java进行测试.
I'm testing from Java with JUnit/DBUnit.
BR 弗兰克
推荐答案
只要您的体重在10克或更高,有一种方法可以做到这一点.这称为条件编译.这是一个非常简洁的功能,提供了特殊的语法,因此我们可以在编译时更改我们的PL/SQL代码.
There is a way to do this, providing you are on 10g or higher. It's called Conditional Compilation. This is a highly neat feature which provides special syntax so we can change our PL/SQL code at compilation time.
碰巧的是,我一直在精确地使用此功能来公开规范中的私有软件包,因此我可以针对它们运行UTPLSQL测试.
As it happens I have been using this feature precisely to expose private packages in a spec so I can run UTPLSQL tests against them.
这是特殊的语法:
create or replace package my_pkg
as
$IF $$dev_env_test $THEN
PROCEDURE private_proc;
$END
FUNCTION public_function return date;
end my_pkg;
/
带有双美元符号的变量是条件编译标志.
That variable with the double-dollar sign is a Conditional Compilation flag.
如果我描述该软件包,我们只能看到公共软件包:
If I describe the package we can only see the public package:
SQL> desc my_pkg
FUNCTION PUBLIC_FUNCTION RETURNS DATE
SQL>
现在,我设置条件标志并重新编译程序包,就好像被魔术...
Now I set the conditional flag and re-compile the package, and as if by magic ...
SQL> alter session set plsql_ccflags='dev_env_test:true'
2 /
Session altered.
SQL> alter package my_pkg compile
2 /
Package altered.
SQL> desc my_pkg
PROCEDURE PRIVATE_PROC
FUNCTION PUBLIC_FUNCTION RETURNS DATE
SQL>
私有化功能就像您想的那样简单:
Privatising the functions is as simple as you think it would be:
SQL> alter session set plsql_ccflags='dev_env_test:false'
2 /
Session altered.
SQL> alter package my_pkg compile
2 /
Package altered.
SQL> desc my_pkg
FUNCTION PUBLIC_FUNCTION RETURNS DATE
SQL>
我们可以通过条件编译做更多的事情.它涵盖在文档中. 了解更多信息.
We can do lots more with conditional compilation. It's covered in the docs. Find out more.
这篇关于有没有一种方法可以访问私有的plsql过程以进行测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!