insomnia icon indicating copy to clipboard operation
insomnia copied to clipboard

Edit environment variables programmatically in Unit Tests

Open lanphan opened this issue 3 years ago • 21 comments

What is your question? A clear and concise description of what you are wanting to ask. Ex. I'm always frustrated when [...] Environment variable and template tags are very powerful in Collections, but I don't find any document about their usage in Unit testing. Is it possible to use them in Unit testing? In addition, I'd like to get some data from external apis to automate some testing scenarios, do Unit testing support it?

What is the context? If applicable, describe the context in which you are asking the question. Ex. I am trying to do x with y [...] To support table-driven testing, I'd like to define a static list of data before executing tests. In some special cases, these data can be got from external apis which I'd like to call to get that data to use, therefore, I need to call external api in unit testing.

lanphan avatar Mar 20 '21 12:03 lanphan

In addition, I'd like to get some data from external apis to automate some testing scenarios, do Unit testing support it?

well.. if you're asking me to put my leet-haxor hat on, you can do something like this

const http = require('http')
const google = await new Promise(resolve => {
  http.get('http://google.com', resolve);
});
expect(google.readable).to.equal(true);

But I can't say I'd recommend it. Not sure of another way, though. We don't expose axios (or any other dependency) to the test itself, from what I can tell.

dimitropoulos avatar Mar 23 '21 13:03 dimitropoulos

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar May 26 '21 23:05 stale[bot]

I am also looking for a way to access variables in unit testing that I can access in the debug tab. Is there a way to do this?

I'm testing an API that needs to use generated values for some imputs since the backend of the service doesn't allow duplications of certain data in the database. But that makes it hard (read: impossible) to do any assertions on the data that I'm receiving in the responses.

sillen102 avatar Aug 03 '21 14:08 sillen102

+1 how to access and modify variables, request body and request query params in unit testing. Currently unit testing is not very useful because only one test is possible. No way to test multiple edge cases with different sets of data per request. Basically I need something like this: image

mugi-luffy avatar Feb 03 '22 19:02 mugi-luffy

@lanphan @sillen102 at the moment we don't expose this functionality and allow you to for example tinker with the environment variables, similar to what Postman allows programmatically in their Pre-Request and Test code in scripts.

@mugi-luffy what you mention is also not supported, but is catalogued as improvement here: https://github.com/Kong/insomnia/issues/4482

We are at the moment gathering all this kind of feedback to try and come up with a feasible plan to improve the test feature.

Feel free to report any other issues or improvements you notice 👍

filfreire avatar Jul 25 '22 13:07 filfreire

@filfreire, do you have an ETA on this feature?

suuunly avatar Oct 03 '22 09:10 suuunly

👍 +1

Just discovered insomnia couple hours ago and it feels like it knows exactly what I need and has that! The only exception so far ... accessing environment variables in unit tests. I need the test to have different expectations on different environments (seems quite natural). Any chance to get that? (Or would it be possible to write a plugin?)

ya55en avatar Oct 06 '22 18:10 ya55en

👍 +1

nnickodem avatar Nov 02 '22 19:11 nnickodem

Yeah, I will +1 this one as well.

Programmatically being able to access:

  • Request information
  • Environment variables

Is very useful in tests.

Why?

Environment variable access means I don't need to duplicate data between env vars and unit tests

const response1 = await insomnia.send();
const responseData = JSON.parse(response1.data);
expect(response1.valueOnResponse).to.equal(insomnia.environment.valueInEnvVars);

Information inside the request can be validated against the response data

const request = insomnia.currentRequest;
const requestData = JSON.parse(request .data);
const response1 = await insomnia.send();
const responseData = JSON.parse(response1.data);
expect(response1.valueX).to.equal(requestData valueX);

domingoladron avatar Nov 07 '22 06:11 domingoladron

👍 +1

PierreStainton avatar Nov 08 '22 10:11 PierreStainton

+1 !

LucasDSVelco avatar Nov 15 '22 08:11 LucasDSVelco

+1

ivanandriollo avatar Dec 14 '22 09:12 ivanandriollo

+1

Skazakstan avatar Feb 10 '23 08:02 Skazakstan

+1 I think this feature is a must have. Thanks for the good work on insomnia by the way.

pdomineaux avatar May 11 '23 13:05 pdomineaux

Im still not seeing a way to edit or change Envrionment Veriables in the Test tab. I have Variables that get submitted via the URL and I can not write tests to change that info Or multiple similar tests with Different info for those variables. #4482 did not solve the problem.

Also, Its seems a bit odd that the Test tab doesnt have a way to select an envronment.

chaz4short avatar Jun 20 '23 20:06 chaz4short

Big +1 for this. Ideally we should be able to create one request with some part of it that varies. For example I'm testing a content search API where I want to parameterize the query text in unit tests to check the results.

I shouldn't have to create many different requests that are otherwise identical to do this. I should be able to change them in the unit test suite before launching each request.

smacktoid avatar Aug 03 '23 16:08 smacktoid

+1

tochkam avatar Nov 03 '23 16:11 tochkam

+1

ntijoh-mortaza-ebeid avatar Dec 03 '23 13:12 ntijoh-mortaza-ebeid