Is there a good way to use Karate for tests that depend on the state of the database?

霸气de小男生 提交于 2021-02-17 02:52:14

问题


Consider for example we have scenarios that test basic CRUD operations for rows in a specific relational database table (using endpoints from a web server). We then have another scenario that returns all of the rows in that table, using an endpoint called 'getAll' or something to that effect. Certainly in karate we can verify that the 'shape' of the response is how we expect using something like

And match response[*] == #something

But is there a way to actually verify that we get back the expected number of rows? In short, is there a way to strictly control our test data so that we know precisely what is in the database table at the beginning of the test? Certainly we could do this using Java interop if we could run a scenario atomically, but as far as I know there is currently no way to do this. And, if we are not running atomically, due to the parallel nature of karate tests (by default), I don't see a way to ensure that we know the state of the database throughout an entire test, without running all of the karate scenarios sequentially.


回答1:


In case you haven't seen the dogs.feature in the demos:

But looks like you have and you are aware of Java interop. The question you ask is interesting, it is the first time I heard of the parallel execution capability being a "problem".

Normally teams I have seen write tests that are scoped to a single entity (e.g. customer, order) that was created in the same Scenario therefore not dependent on any other test in your suite.

Also, nothing stops you from setting number of threads to 1.

And do be aware that within a Feature you can request all Scenario-s to run in sequence: https://github.com/intuit/karate#parallelfalse

Anyway if you really want to go down this path, here's what I suggest. You can write a Java helper to lock your database, run a few steps and unlock, how you do this is up to you.

Also note that karate.callSingle() will actually lock across all threads. This is normally used to do a "set up" routine, such as get an auth token for all your tests. In your case you could take a "snapshot" of your DB so that your tests can derive expected results. But you can experiment with it if it helps.

Otherwise please assume that Karate doesn't support what you are asking for. Note that splitting a logical "flow" into multiple "Scenario-s" is something I thoroughly discourage: https://stackoverflow.com/a/46080568/143475



来源:https://stackoverflow.com/questions/61915930/is-there-a-good-way-to-use-karate-for-tests-that-depend-on-the-state-of-the-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!