单元测试 DBFit

 

https://github.com/dbfit/dbfit

http://dbfit.github.io/dbfit/docs/reference.html#connect-using-file

http://dbfit.github.io/dbfit/docs/

http://dbfit.github.io/dbfit/docs/getting-started.html#creating-a-new-test-page

 

1. Installing DbFit

DbFit needs the Java Runtime Environment to run - it can be downloaded from the  Oracle's Java homepage.
  1. Download DbFit

  2. Unpack dbfit-complete-XXX.zip somewhere on your disk, and run startFitnesse.bat (or startFitnesse.sh on Linux). When FitNesse starts, you should see a command window with this message:

    FitNesse (v20121220) Started...
            port:              8085
            root page:         fitnesse.wiki.FileSystemPage at ./FitNesseRoot
            logger:            none
            authenticator:     fitnesse.authentication.PromiscuousAuthenticator
            page factory:      fitnesse.responders.PageFactory
            page theme:        fitnesse_straight
            page version expiration set to 0 days.
    
  3. Open http://localhost:8085/ and you should see the welcome page. FitNesse is up and running. When you want to shut it down later, just press Ctrl+C in the command window (or close the command window).

    FitNesse.bat failed. What's wrong?

    Read the exception from the command window. If the error mentions versions, check that you have Java 5 or higher installed and that the correct version is being executed when you run java.exe. Run java.exe -version from a command window to see which version of Java is being executed by default. You can run FitNesse with a different Java version either by pointing to the correct JRE in the system executable path (right-click My Computer, select Properties, then go to the Advanced tab, click Environment Variables, and edit the Path variable), or by entering the full path to a different java.exe in startFitnesse.bat.

    If the error report states that there is a security problem or the port is unavailable, enter a different port number instartFitnesse.bat and try again.

Let’s run a quick test to make sure that you have everything set up correctly and that FitNesse can connect to your test database.

2. Creating a new test page

Open http://localhost:8085/HelloWorldTest in your browser. You should see an editor - this is where we’ll create our new test page.

Note Notice that the page name is a CamelCase word.

In FitNesse, all page names have to start with a capital letter, have at least one more capital letter, and all capital letters have to be separated by at least one lowercase letter. FitNesse is really strict about that. This convention causes a lot of headaches for FitNesse newbies, but after a while you'll get used to it. Here are some good page names:
  • HelloWorld
  • TestFluxCapacitor
  • IsPaymentWorkingCorrectly
Here are some page names that will get you in trouble:
  • helloworld (no capital letters)
  • Testfluxcapacitor (just one capital letter)
  • isPaymentWorkingCorrectly (starts with a lowercase letter)
  • TestFCapacitor (two consecutive capital letters)

3. Setting up the environment

In order to load the DbFit extension into FitNesse, your test pages have to load the correct libraries. Replace the contents of the big textbox with the following:

!path lib/*.jar

4. Connecting to the database

DbFit requires two commands to connect to the database. The first line specifies the database type (or test type), and the second defines connection properties. These two lines will typically be the first on every test page. Here is how to connect to a MySQL database:

!|dbfit.MySqlTest|

!|Connect|localhost|dbfit_user|password|dbfit|

Notice the MySqlTest in the first line above. That tells DbFit which type of database driver to use.

Here’s a comprehensive list of available types:

DatabaseDbFit Database Type
OracleOracleTest
SQL Server 2005 and laterSqlServerTest
MySQLMySqlTest
PostgresPostgresTest
DerbyDerbyTest
HSQLDBHSQLDBTest
DB2DB2Test
DB2iDB2iTest
Notice how each command starts with an exclamation mark ( !), followed by a pipe symbol ( |). Command arguments are then separated by the pipe symbol as well. In FitNesse, tables are used to describe commands, tests, inputs and expected results (you will see the table when the page is saved). In the FitNesse wiki syntax, tables are described simply by separating cells with the pipe symbol. The exclamation mark before the first row of the table is optional, and tells FitNesse not to apply any smart formatting to table contents.

5. Testing a simple query

Now let’s write a simple query test. We will send a request to the database, pull out the result set, and compare it with our expectations. In DbFit, that is done with the Query command. The second cell of the first table row, after the Query keyword, should contain the query we are executing. The second row then contains the result set structure — names of the columns that we want to inspect. You don’t have to specify the full result set here, just the columns that are interesting for a particular test. All rows after that contain expected results. Query disregards result set order — if the order is important you can use OrderedQuery. Here is a simple MySql query:

!|Query| select 'test' as x|
|x|
|test|

The same syntax should work for SQLServer. For Oracle, use this table:

!|Query| select 'test' as x from dual|
|x|
|test|

6. Running the test

Now, click Save. FitNesse will create a new page and display it in your browser. Click on the Test button to make FitNesse run the test. You should see a page telling you that the test passed.

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
《Step By Step写测试》介绍使用 JTESTER 写单元测试,作者吴大瑞,pdf 格式,大小 1.8MB。本书带有 pdf 书签,方便读者阅读。 目录: Jtester的项目配置 1 TestNG测试 1.1 一个简单到不能再简单的测试 1.2 在eclipse中运行testng测试 1.3 定义测试组 1.4 生命周期 1.5 在testng中使用jtester 2 断言的使用 2.1 所有对象都支持的断言 2.2 number和string对象支持的断言 2.3 array和collection对象支持的断言 2.4 string对象支持的断言 2.5 map对象支持的断言 2.6 data/calendar对象支持的断言 2.7 file对象支持的断言 2.8 FAQ 3 Spring容器的加载 3.1 基本功能演示 3.2 让框架自动注册spring bean 3.2.1 简单示例 3.2.2 @AutoBeanInject规则详解 3.2.3 Spring Bean依赖项注入规则 3.2.4 注册特殊实现类 3.3 DIY Spring Bean 3.4 Spring总结 4 JMockit使用 4.1 jmockit的优点 4.1.1 传统mock方法的限制: 4.1.2 使用mock的场景 4.1.3 运行jmockit测试 4.2 基于状态的jmockit测试 4.2.1 方法一:使用@MockClass和 @UsingMocksAndStubs 4.2.2 方法二:使用Mockit.setUpMock() 4.2.3 方法三:In-line mock classes 4.2.4 使用it来访问mock对象本身 4.3 基于行为的jmockit测试 4.3.1 Mock对象 4.3.2 mockit.Expectations 4.3.3 声明和使用mock对象 4.4 Mock对象的约束 4.4.1 调用次数约束 4.4.2 参数约束 4.4.3 返回值的指定 4.5 Fluent语法 4.6 部分mock行为 4.7 Jmockit和spring的集成 4.7.1 @MockedBean 4.7.2 Mock部分springbean行为 4.8 访问私有的方法,构造函数或变量 4.9 @Inject注解 5 数据库测试 5.1 jtester.properties文件配置 5.2 使用@DbFit功能来进行数据库测试 5.3 @DbFit的wiki语法 5.4 数据库连接 5.5 查看执行结果 5.6 清空表数据 5.7 查询数据(Query) 5.8 有序查询(ordered query) 5.9 使用参数 5.10 插入数据(Insert) 5.11 执行SQL语句(Execute) 5.12 commit&rollback; 5.13 其它命令 5.14 事务管理 5.15 Martini项目下的ibatis文件配置 5.16 数据库测试FAQ 6 Spring和SQL跟踪 6.1 @Tracer 6.2 FAQ 7 JTester插件的使用 7.1 插件功能 7.2 插件安装 7.3 录制变量的功能 7.4 dbFit插件编辑功能 7.5 从数据库中直接拖取数据 7.6 使用SQL Editor编辑器 覆盖率统计

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值