1

我有将数据写入数据库的 Java 批处理应用程序。我想要一个自动化测试,它将预期输出与数据库中的数据进行比较。我使用 DBfit 完成了这个过程并且工作得很好,除了我必须在 excel 电子表格中输入我的预期数据,然后复制并粘贴到 dbfit 并格式化它,所以这是一个我想避免的手动步骤。

是否有任何自动化工具可以为 java 批处理应用程序执行功能测试?

4

1 回答 1

1

我使用 DOS 批处理脚本编写了一个数据库比较测试工具。此脚本将数据库模式与其先前的基线进行比较以发现差异。可能能够被修改以比较实际的表数据内容。这是我的代码,您可能需要稍作修改才能使其正常工作。显然,要回答您的问题,一个简单的 JUnit 或 NUnit 脚本可以启动它。

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
ECHO.&ECHO.
ECHO This script will get schema snapshots of all your local MSSQL databases
ECHO and saves them to a directory where this script is ran.
PAUSE
CLS
:: first get timestamp of this script
SETLOCAL
FOR /F "skip=1 tokens=2-4 delims=(-)" %%a IN ('"echo.|date"') DO (
    FOR /F "tokens=1-3 delims=/.- " %%A IN ("%DATE:* =%") DO (
        SET %%a=%%A&SET %%b=%%B&SET %%c=%%C))
SET /A "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
FOR /F "tokens=1-4 delims=:. " %%A IN ("%time: =0%") DO @SET UNIQUE=%yy%%mm%%dd%-%%A%%B

SET TITLE=atabase schema snapshot
TITLE=D%TITLE%
:: script also supports DBNAME as argument
IF NOT "%1"=="" (
  SET DBNAME=%1
  %JAVA_HOME%\bin\java.exe -classpath jtds-1.2.4.jar;schemacrawler-8.8.jar;schemacrawler-sqlserver-8.8.jar schemacrawler.tools.sqlserver.Main^
 -user=sa -password=password -database=!DBNAME! -schemas=!DBNAME!.dbo.* -port=1433 -host=localhost -table_types=TABLE -command=schema -sortcolumns=true^
 -procedures= -infolevel=lint -loglevel=OFF >!DBNAME!_schema_!UNIQUE!.txt
  GOTO :ENDSINGLE
)

:: enable this section to run minimized
::IF NOT DEFINED PIL (
::    SET PIL=1
 ::   START /MIN "" %~0 %1
 ::   EXIT /B
::)

:BEGIN
ECHO D%TITLE% is running...
ECHO.
MKDIR %UNIQUE%
ECHO Directory %CD%\%UNIQUE% has now
ECHO been created to hold the results of this snapshot.
ECHO.
sqlcmd.exe -E -Slocalhost -h-1 -Q"SET NOCOUNT ON;SELECT name FROM sysdatabases WHERE name NOT IN ('master','tempdb','model','msdb');" >dblist.tmp
FOR /F %%A IN (dblist.tmp) DO (
  SET str=%%A
  SET str=!str: =!
  ECHO !str!>>.\!UNIQUE!\dblist_trimmed.tmp
)
ECHO.&ECHO.&ECHO.
FOR /F %%I IN (.\!UNIQUE!\dblist_trimmed.tmp) DO (
  SET DBNAME=%%I
  IF NOT "!DBNAME!"=="" (
    ECHO. Snapshot: !DBNAME!
    %JAVA_HOME%\bin\java.exe -classpath jtds-1.2.4.jar;schemacrawler-8.8.jar;schemacrawler-sqlserver-8.8.jar schemacrawler.tools.sqlserver.Main^
 -user=sa -password=password -database=!DBNAME! -schemas=!DBNAME!.dbo.* -port=1433 -host=localhost -table_types=TABLE -command=schema -sortcolumns=true^
 -procedures= -infolevel=lint -loglevel=OFF >.\!UNIQUE!\!DBNAME!_schema.txt
  )
)
DEL /Q dblist.tmp
GOTO :ENDMULTI

:ENDSINGLE
ECHO Finished processing %1 .  Closing in 20 seconds...
GOTO :END
:ENDMULTI
ECHO Finished processing list .  Closing in 20 seconds...
:END
ECHO.
FOR /l %%a in (20,-1,1) do (TITLE D%TITLE% -- closing in %%as&ping -n 2 -w 1 127.0.0.1>NUL)
于 2012-09-28T18:01:44.143 回答