karatelabs / karate

Test Automation Made Simple
https://karatelabs.github.io/karate
MIT License
8.33k stars 1.95k forks source link

Make karate.scenario available to use in mock feature files #2618

Open maggiesid opened 1 month ago

maggiesid commented 1 month ago

As proposed in this StackOverflow thread started by a teammate of mine https://stackoverflow.com/questions/78927512/select-mock-file-to-use-based-on-the-running-karate-scenario

problem: I have some Karate Scenarios to test my application and I'm using a Karate MockServer as mocked downstream service. To simulate edge case scenarios (e.g. empty results from downstream), I want to select a specific mock file based on the running Karate Scenario. Is there a way to know which is the Scenario that triggered the downstream mock call? We have been suggested by Peter Thomas to use karate.scenario but currently it does not work for mocks We would need to extend karate to allow the usage of it in mock files as well

ptrthomas commented 1 month ago

@maggiesid will tag this as help wanted as no one has asked for this so far. I recommend that you or someone in your team looks at the code and contributes a PR, it should be easy

BTW I think the header approach you seem to be using now as per the stack-overflow post is a reasonably good one

maggiesid commented 1 month ago

Hi @ptrthomas thank you very much for your quick reply! The header approach that was posted in that Stack Overflow thread was considered first, but then we decided it wasn't a good fit for us because of how our codebase work, so we would have to proceed with contributing to Karate

Do you have any hint about where to contribute in Karate codebase to make karate.scenario available in mock feature files? Thank you in advance for your cooperation

ptrthomas commented 1 month ago

@maggiesid I'll try. the mock handling happens here: https://github.com/karatelabs/karate/blob/v1.5.0/karate-core/src/main/java/com/intuit/karate/core/MockHandler.java#L157

initEngine() sets up a dummy scenario, so maybe some way to update that to reflect the final resolved scenario ...

maggiesid commented 1 month ago

Hi @ptrthomas I have been investigating this and I have one doubt:

Problem is that I would need to print, inside a mock feature file scenario (M1.feature), the name of the test scenario that I am executing in T1.feature

`// T1.feature Scenario: scenario 123 ...

// M1.feature Scenario: pathMatches('/downstream/path/api/v0')`

So I would need to print "scenario 123" while I am executing scenario pathMatches('/downstream/path/api/v0') of M1.feature file

Given that MockServer objects are started while setting up the testsuite running, independently from the scenarios

I see that the MockServer object runs independently by the test scenario I am executing When I am in "handle" method of MockHandler and I could be able to set name information in runtime scenario object (that then appears in karate.scenario.name), I see that I have only visibility of the downstream request that I am intercepting. But I don't see having any visibility of the original scenario in the feature file (T1) that the request is originated by. Is there any shared context object between the MockServer and the test I am executing?

Thank you again for your cooperation

ptrthomas commented 1 month ago

yes mocks are designed to be independent. if you really want some connection between some test and the mock, either you continue to use something in the HTTP request (like headers which you are already doing) or you have no option but to build this integration yourself. this sample should give you enough pointers: https://x.com/getkarate/status/1417023536082812935