I use mocha with npm request library for API tests. I have one root level before hook in my mocha tests in which I generate tokens for different users and then store them in the global variables like: (the bellow list is generate in the before hook on root level)
- global.token1=token1,
- global.token2.token2
The idea is to generate the tokens just one time for all tests accross different specs, and just use them when needed. Some actions might be done only by admin user.
These tokens are visible in the spec file:
-
in the "before hook"
-
in "it" block
-
but NOT in the "describe" block
I need to run same tests in spec for different users (different tokens). And here I have no idea how efficiently achieve it? I just do not want to have 2 the same tests in spec which differs just by the different token. I thought about for loop, but tokens from globals are not visible in the describe block. snippet code would be:
let tokens = { user: global.userToken, admin: global.adminToken }; describe('Run sample test scenario for different roles', function () { console.log(global.userToken, 'Here I can NOT see the global.userToken'); for (const token in tokens) { describe(`The same test for different ${token} user`, function () { it('some step 1', function () { console.log(global.userToken, 'Here I can see the global.userToken'); api.get(path, tokens[token]); }); }); } });
How can I achieve this? Is there some other maybe better way than trying storing some variable in the global?
Aucun commentaire:
Enregistrer un commentaire