0

因此,我正在尝试存根数据库连接器以便为其编写测试。问题是数据库连接器是一个非常薄的层,对它的查询有点开放。我希望我的代码能够从数据库中请求一个变量,并且连接器应该可以接受。此调用的签名如下所示:

dim = getDimension(self,dimensionName,otherIndentifyingInformation)

(这一切都在 MATLAB 中——希望答案与语言无关,或者至少在 MATLAB 中不是不可行的。)

当我在我的代码中使用它时,dimensionName它是存储在数据库中的少数东西之一。如果代码调用它,但它不存在,那很好。这里的问题是我想测试一些对 进行两次调用的代码,getDimension例如:

alt = conn.getDimension('alt',otherID);
str = conn.getDimension('str',otherID);

出于(希望)显而易见的原因,alt并且str不保证相同。事实上,它们通常不是。

所以,我的问题。如果我想存根 getDimension 以返回良好的测试值,我该怎么做?创建getDimensionAlt似乎很笨拙,因为可以从数据库中出来的东西的数量是无限的,维护起来会很痛苦。有没有比将逻辑放入我的存根对象更好的方法?这似乎是错误的处理方式......

编辑:建议设置一个 testDB。那么我不是必须为每个测试用例设置一个 testDB 吗?在每个测试中,我都必须创建一个数据库连接,将其作为存根返回,运行测试,然后清理数据库连接。这似乎对每个测试来说都是很多开销,尤其是当它甚至不是我正在测试的系统时。

我想可以设置一个 testDB,并每次都用适当的值填充它。这是好习惯吗?

编辑2:也许我的问题不清楚。我有一小段代码正在尝试测试。它并不比上面那两行复杂多少,我想干净利落地测试一下。问题是存根getDimension调用取决于参数。我不需要将此存根与其他测试重用。

I think the answer might be "It's OK to have simple logic in your stubs." This is all confounded by the fact that there's no anonymous classes or stubbing framework in MATLAB, so that's hard, but I want to make sure what I'm doing is conceptually clear before I go off and write a stubbing framework in MATLAB.

4

3 回答 3

0

Could you set up a a test database with known values and then perform a known set of queries against it (with known expected return values)?

于 2009-03-10T13:06:00.143 回答
0

I think setting up a testdb is a good idea if you're doing automated integration tests or acceptance tests. This usually calls for tests where you test the whole stack (UI, application, database, services etc.) at once.

Unit testing is quite different, they are used during development and not during testing, therefore they should be quicker to run, quicker to change and quicker to write. Its usually a good idea to have unit tests only depend on the small piece of code they're testing. This makes them less brittle, easier to understand and makes developers more willing to run them often.

Using stubs in unit tests is a good idea. The problem you seem to run in to is that if you want to reuse stubs for many tests their behaviour can get very complex, in the worst case you're rebuilding the thing they are supposed to stand in for. This can also happen if you are testing too much at one time.

The solution to this is to have more smaller stubs that only implement the behaviour for one or a few tests and have your tests only test a single behaviour. Tests that test lots of behaviour at once are useful too but they're not unit tests, you should move them to your integration test suite (and can run on a real database). I'm not sure about matlab but in many languages you have mocking frameworks you can use to handle creating stubs and setting up behaviour for you.

于 2009-03-10T13:43:26.840 回答
0

My answer on a similar question Creating mock data for unit testing:

You can have Builder class(es) that helps you building the instances you need / in this case ones you would use related to the repository. Have the Builder use appropiate defaults, and on your tests you can overwride what you need. This helps you avoid needing to put have every single case of "data" mixed up for all the different tests (which introduces problems, because usually there are cases that aren't compatible for different tests).

Update 1:Take a look at www.markhneedham.com/blog/2009/01/21/c-builder-pattern-still-useful-for-test-data

Also check this answer, on the overall unit test relation to external systems: How can I improve my junit tests

Note there is an alternative using in-memory databases like dbunit. I prefer the builder, for the reasons mentioned in the later link.

于 2009-03-10T16:50:03.037 回答