# Asserting over a dynamic boolean

**URL:** <https://community.postman.com/t/asserting-over-a-dynamic-boolean/54784>\
**Category:** Help Hub\
**Tags:** variables\
**Created:** [November 19, 2023, 7:31pm UTC](https://community.postman.com/t/asserting-over-a-dynamic-boolean/54784 "2023-11-19T19:31:59Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![hannahbroch](https://avatars.discourse-cdn.com/v4/letter/h/ecae2f/32.png) [@hannahbroch](https://community.postman.com/u/hannahbroch)\
**Post date:** [November 19, 2023, 7:32pm UTC](https://community.postman.com/t/asserting-over-a-dynamic-boolean/54784/1 "2023-11-19T19:32:00Z")

</div>

i have a dynamic boolean in my code  
i want to assert over the response body  
i used the following:-

```auto
var trackExplicitLyrics = pm.collectionVariables.get("explicitLLanguageIdBoolean") == true
pm.test("Explicit language field modified successfully", function () {
    pm.expect(jsonData.tracks[0].explicit).to.eql(trackExplicitLyrics);
  
});

```

When the boolean in the response is true, i rcv the following error  
| AssertionError: expected true to deeply equal false

When the boolean in the response is false, the test passes

How can i write a test that will always pass as long as the response body returns the correct boolean that was set in the pre-request script  
thanks

---

<div class="post-metadata">

**Author:** ![hannahbroch](https://avatars.discourse-cdn.com/v4/letter/h/ecae2f/32.png) [@hannahbroch](https://community.postman.com/u/hannahbroch)\
**Post date:** [November 21, 2023, 6:35pm UTC](https://community.postman.com/t/asserting-over-a-dynamic-boolean/54784/2 "2023-11-21T18:35:22Z")

</div>

can anyone help me please?

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [November 21, 2023, 7:06pm UTC](https://community.postman.com/t/asserting-over-a-dynamic-boolean/54784/3 "2023-11-21T19:06:36Z")

</div>

Hey @hannahbroch 👋

Could you share an example of the response body which shows that true/falue value, please?

Can you expand on what you would have in the pre-request script that would be changing the boolean value? Not sure I get that part.

---

<div class="post-metadata">

**Author:** ![hannahbroch](https://avatars.discourse-cdn.com/v4/letter/h/ecae2f/32.png) [@hannahbroch](https://community.postman.com/u/hannahbroch)\
**Post date:** [November 21, 2023, 9:38pm UTC](https://community.postman.com/t/asserting-over-a-dynamic-boolean/54784/4 "2023-11-21T21:38:21Z")

</div>

@danny-dainton

thanks

response body sample:-

```auto
{
    "labelId": 293312,
    "labelName": "Identity - CHILD",
    "discNumber": null,
    "trackLength": null,
    "channels": null,
    "sampleRate": null,
    "bitDepth": null,
    "bitrate": null,
    "trackVendorId": null,
    "isrc": null,
    "version": null,
    "copyrightC": null,
    "copyrightP": null,
    "description": null,
    "languageId": 175,
    **"explicit": false,**
    "lyrics": null,
    "playingCount": 0,
    "appleId": null,
    "priceTierId": null,
    "rdioId": null,
    "previewStartSeconds": null,
    "totalSales": 0.0,
    "image": null
}

```

prerequest script:

```auto
pm.collectionVariables.set("explicitLanguage1", pm.variables.replaceIn('{{$randomBoolean}}'));

```

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [November 22, 2023, 10:40am UTC](https://community.postman.com/t/asserting-over-a-dynamic-boolean/54784/5 "2023-11-22T10:40:08Z")

</div>

Hey @hannahbroch,

I still don’t really understand why the _pre-request script_ is being used to set a variable for the boolean value 🤔

Do you not want to create the assertion based on the response body and the `explicit` property value?

If you’re looking at checking that the response contains that property and it’s a specific value, you could do this:

Response:

```json
{
    "tracks": [
        {
            "labelId": 293312,
            "explicit": true
        }
    ]
}

```

Test:

```auto
let jsonData = pm.response.json();
pm.test("Explicit language field is true", function () {
    pm.expect(jsonData.tracks[0].explicit).to.be.true;
});

```

It’s difficult to see/know what you’re actually testing for here. I don’t understand the use case of checking the response against a random variable value created in a pre-request script.

---

<div class="post-metadata">

**Author:** ![hannahbroch](https://avatars.discourse-cdn.com/v4/letter/h/ecae2f/32.png) [@hannahbroch](https://community.postman.com/u/hannahbroch)\
**Post date:** [November 22, 2023, 11:05am UTC](https://community.postman.com/t/asserting-over-a-dynamic-boolean/54784/6 "2023-11-22T11:05:04Z")

</div>

@danny-dainton  
in the request, the user has the option to select true or false  
i want to be able to test both options at random, sometimes they choose true and sometimes they choose false

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [November 22, 2023, 11:41am UTC](https://community.postman.com/t/asserting-over-a-dynamic-boolean/54784/7 "2023-11-22T11:41:26Z")

</div>

Wouldn’t you want to test the actual response you’re going to be receiving from the user, rather than have a test that’s not really checking that?

Could you not add some logic in the Tests to test for a certain value based on the response.

You can do something like this:

```javascript
let jsonData = pm.response.json();

if (jsonData.tracks[0].explicit) {
    pm.test("Explicit language field is true", function () {
        pm.expect(jsonData.tracks[0].explicit).to.be.true;
    });
}
else {
    pm.test("Explicit language field is false", function () {
        pm.expect(jsonData.tracks[0].explicit).to.be.false;
    });
}

```

Or have check that it’s either true or false and add the value to the test result :

```javascript
let jsonData = pm.response.json();

pm.test(`Explicit language field is true or false - Value is ${jsonData.tracks[0].explicit}`, function () {
    pm.expect(jsonData.tracks[0].explicit).to.be.oneOf([true, false]);
});

```

There are lots of things you could do and assert against.

* * *

If you wanted to test the dynamically changing response value, you can create the same variable in the pre-request script:

```auto
// _.sample() is a Lodash function that randomly picks a value
pm.collectionVariables.set('explicitValue', _.sample([true, false]));

```

And add that variable to the request payload body:

```auto
{
    "tracks": [
        {
            "labelId": 293312,
            "explicit": {{explicitValue}}
        }
    ]
}

```

---

<div class="post-metadata">

**Author:** ![hannahbroch](https://avatars.discourse-cdn.com/v4/letter/h/ecae2f/32.png) [@hannahbroch](https://community.postman.com/u/hannahbroch)\
**Post date:** [November 22, 2023, 11:54am UTC](https://community.postman.com/t/asserting-over-a-dynamic-boolean/54784/8 "2023-11-22T11:54:06Z")

</div>

thank you very much, this is very helpful

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [November 22, 2023, 4:33pm UTC](https://community.postman.com/t/asserting-over-a-dynamic-boolean/54784/9 "2023-11-22T16:33:45Z")

</div>

Personally, I really don’t like the concept of dynamic tests.

You should be in control of your test data, and the request\response and associated assertion should be the same every time.

Dynamic tests can be brittle and lead to many false positives (and vice versa).

Therefore, you should have two separate requests (at the minimum).

One for true, one for false, and potentially one for an invalid option that should produce an error of some sort.

This really comes down to what you are trying to test.

You can either create two requests for this to the same end point with the appropriate tests, or you could potentially use a pre-request script and tests tab to loop over the options using the array shift() method and setNextRequest.

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [November 22, 2023, 4:54pm UTC](https://community.postman.com/t/asserting-over-a-dynamic-boolean/54784/10 "2023-11-22T16:54:26Z")

</div>

I agree - It’s the feelings I had when I was trying to get my head around the use case and what was actually being tested.

Trying to do everything in a single test, made me feel uneasy. I’m not a fan of dynamic tests. They should be deterministic and checking the same thing each time, so that you know that when it doesn’t fail - you need to be looking at that.

Checks like these are just little change detectors, alerting you to a change in the known behavior.

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [November 22, 2023, 4:54pm UTC](https://community.postman.com/t/asserting-over-a-dynamic-boolean/54784/11 "2023-11-22T16:54:43Z")

</div>

Here is a basic example of using setNextRequest.

I’m running this against Postman Echo with a very basic request body.

![image](https://us1.discourse-cdn.com/getpostman/original/3X/4/b/4bd4fb027fbec1954ec7de8d9cc4039d131fc2a3.png)

With the following in the pre-request scripts…

```auto
if (!pm.collectionVariables.get("booleans")) {
    pm.collectionVariables.set("booleans", JSON.stringify([]));
}

let array = JSON.parse(pm.collectionVariables.get("booleans"));

if (array.length === 0) {
    array = [true, false];
}

pm.test("boolean array is not empty", () => {
    pm.expect(array).to.be.an("array").that.is.not.empty;
});

var currentBoolean = array.shift();
console.log(currentBoolean);

pm.collectionVariables.set("bool", currentBoolean);
pm.collectionVariables.set("booleans", JSON.stringify(array));

```

This would probably answer your original question, as you can use the bool variable for your assertion and it would keep resetting the Boolean array, so it won’t be random, but will go, true, false, true, false, indefinitely.

However, to loop though both options, you can do this using the following in the tests tab.

```auto
var array = JSON.parse(pm.collectionVariables.get("booleans"));

var expectedResponse = pm.collectionVariables.get("bool");

var actualResponse = pm.response.json().data.Boolean;

pm.test(`response boolean = ${expectedResponse}`, () => {
    pm.expect(actualResponse).to.eql(expectedResponse); 
});

if (array.length > 0){
    postman.setNextRequest("setNextRequest Loop"); // Change to what your request name is

} else {
    postman.setNextRequest(null);
}

```

And the results are in…

![image](https://us1.discourse-cdn.com/getpostman/original/3X/7/e/7e3401dfa0f2bb6e662d7cee3aeeb900a74a123e.png)

One iteration, which has run the same request twice with a matching test.

Please note: You need to run this through the collection runner as setNextRequest() only works with the runner.

On a side note, you would normally get your test to fail which I can’t do as I’m only testing what I sent, so it will always pass.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [December 22, 2023, 4:54pm UTC](https://community.postman.com/t/asserting-over-a-dynamic-boolean/54784/12 "2023-12-22T16:54:45Z")

</div>

Closed on December 22, 2023, 4:54pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
