You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Feature] Add support for test.each / describe.each #7036
You can already do each via running test with forEach:
[{a: 1,b: 1,expected: 2},{a: 1,b: 2,expected: 3},{a: 2,b: 1,expected: 3},].forEach(({ a, b, expected })=>{test(`given ${a} and ${b} as arguments, returns ${expected}`,({ page })=>{expect(a+b).toEqual(expected);})});
If the above does not work for you, please tell us why in the comments.
Note from maintainers
Hey everyone,
We discussed this feature extensively within the team and concluded that using a forEach loop is preferable to test.each. Using the standard JavaScript syntax gives you full IDE support. See the discussion in this StackOverflow post for more details.
Using forEach won't affect parallelism; tests will continue to run in parallel as per our parallelism documentation.
Thank you,
< 3 Playwright Team
Reacted by Olesia, Mauricio Puerta Ospina, Sergio Riveros, Patrick Romo, Radek Bednarik, Joe Bateson, Paresh, Richard Willis, Виктория, mailtogagan and 73 more
Hi,
I am currently using the this approach (loop) for data driven tests and was wondering, is there a way we can make these tests run in parallel? Because right now they are being executed sequentially and for "slow" tests the execution time is increased.
Reacted by suchlike, Antonio, Patrick Romo, Aswini Mishra, buckthebug, Phil Panteleev, Dmitry Bondarenko, judoTsvetan, Rock Chan, Oleksii Burda and 9 more
+1 to Paresh (@partparsh) 's suggestion of parallelization. We have a suite of a single test with 20+ parameters that unfortunately adds a lot of time to our build.
For of does work, but Jest's test.each() is more convenient for us, here's an example:
We have a data-driven test that checks URL redirecting. The test works with an array that represents 3 cases with 2 parameters in each case, e.g:
...but with data-driven tests with a lot of parameters, it's not very convenient and tests look slightly messy.
Reacted by Eman, Dmitry Bondarenko, Patrick Romo, Palak Jethwani, ArtemRudenko, Nelson Henrique, Richard Willis, Tomasz Suwaj, cwilsloft, Ramis Vakazov and 17 more
I don't see any real different between the following two. If anything, the latter even gives me more control - for example, I can skip a test when urlType has some specific value, and more.
In my case below, the hook beforeEach, afterEach runs 5 times per 1 test.
How could we achieve the goal that 1 hook 1 test ? Note that, the beforeEach/afterEach could not move out of the for loop
because I need to get data from array arr to initialize (e.g. log-in with admin, log-in with supervisor...).
I don't see any real different between the following two. If anything, the latter even gives me more control - for example, I can skip a test when urlType has some specific value, and more.
Playwright already allows us to do conditional test.skip inside test method, so i don't think "more control" argument is valid.
That would be nice to have IMO. My argument is more psychological 😄
I do agree there is no difference between having a for loop but test.each keeps you more in a "testing mindset".
Also for consistency.
test.each and describe.each would have an advantage for IDE integrations also. These integrations cannot handle tests inside a regular for-loop, but the *.each syntax could be handled well.
Reacted by Alexander Krolick, Alex Krolick, Steven Broderick, Fabian Jocks and Alex Boyce
Could we have an update on the progress of the ticket regarding the each syntax implementation? Other testing frameworks have already adopted this feature, and it would be beneficial for us to understand the timeline for its integration into our processes.
disclaimer: this is just a gist of how to shape the code setup for what your goals are.
disclaimer2: I quickly whipped this up together, so pick it apart if you want, ya'll can take it and run with it
For the time being we're using a helper tagging function:
exportfunctionfromTable<Sextendsstring,AextendsReadonlyArray<S>,Pextendsany[]>([header]: A, ...values: P): {[KinS]: P[number]}[]{// read header from tableconstheaders=header.split('|').map(h=>h.trim());constrows: Array<Record<S,P>>=[];// gather data from tablefor(leti=0;i<values.length;i+=headers.length){constrow=values.slice(i,i+headers.length);constparams={}asRecord<S,P>;for(letj=0;j<headers.length;j++){params[headers[j]asS]=row[j];}rows.push(params);}returnrows;}
This can be used in a for loop with a template string:
for(const{ a, b, c }offromTable` a | b | c${1} | ${'a'} | ${true}${2} | ${'b'} | ${false} `){test(`Each with fromTable ${a}`,async()=>{expect(typeofa).toBe('number');expect(typeofb).toBe('string');expect(typeofc).toBe('boolean');});}
Reacted by PowerCreek, Alejandro Juárez and Charlotte Clough
+1 to test.each and describe.each with parallelization. The ergonomics of playwright already align with things like testing library and jest, so why not complete the catalog? As an engineer, if I can flow between unit tests and e2e tests without having to reach for different tooling, it makes my life easier.
Reacted by John Wilson, Fabian Jocks, Svyatoslav Zaytsev, Erick Tomaz, Victor Luchian, 3inv, jesus-alvarado, Kęstutis Katkus, EliotHerbstUC, Tino and 4 more
We discussed this feature extensively within the team and concluded that using a forEach loop is preferable to test.each. Using the standard JavaScript syntax gives you full IDE support. See the discussion in this StackOverflow post for more details.
Using forEach won't affect parallelism; tests will continue to run in parallel as per our parallelism documentation.
Here is an example:
[{a: 1,b: 1,expected: 2},{a: 1,b: 2,expected: 3},{a: 2,b: 1,expected: 3},].forEach(({ a, b, expected })=>{test(`given ${a} and ${b} as arguments, returns ${expected}`,({ page })=>{expect(a+b).toEqual(expected);})});
Activity
partparsh commentedon Jun 25, 2021
Hi,
I am currently using the this approach (loop) for data driven tests and was wondering, is there a way we can make these tests run in parallel? Because right now they are being executed sequentially and for "slow" tests the execution time is increased.
[-][Feature] Add support for test.each[/-][+][Feature] Add support for test.each / describe.each[/+]suchlike commentedon Aug 4, 2021
+1 to Paresh (@partparsh) 's suggestion of parallelization. We have a suite of a single test with 20+ parameters that unfortunately adds a lot of time to our build.
pheeel commentedon Sep 22, 2021
For ofdoes work, but Jest'stest.each()is more convenient for us, here's an example:We have a data-driven test that checks URL redirecting. The test works with an array that represents 3 cases with 2 parameters in each case, e.g:
So with Jest, I can write my test like this:
With PW test I have to write like this:
These
data[0],data[1]don't look good, I can reassign them......or use destructuring assignment...
...but with data-driven tests with a lot of parameters, it's not very convenient and tests look slightly messy.
dgozman commentedon Jan 4, 2022
I don't see any real different between the following two. If anything, the latter even gives me more control - for example, I can skip a test when
urlTypehas some specific value, and more.hungdao-testing commentedon Apr 4, 2022
In my case below, the hook
beforeEach, afterEachruns 5 times per 1 test.How could we achieve the goal that
1 hook 1 test? Note that, the beforeEach/afterEach could not move out of thefor loopbecause I need to get data from array
arrto initialize (e.g. log-in with admin, log-in with supervisor...).dgozman commentedon Apr 4, 2022
Hung Dao (@hungdao-testing) Just make a few describes in a loop.
SuwiDeity commentedon Apr 20, 2022
Playwright already allows us to do conditional test.skip inside test method, so i don't think "more control" argument is valid.
4ekki commentedon Aug 29, 2022
+1 for
test.eachnotation. Also, Jest has amazing way of populating data via table, which is very convenient when test case has 3+ parametersw9ahmed commentedon Sep 6, 2022
That would be nice to have IMO. My argument is more psychological 😄
I do agree there is no difference between having a for loop but
test.eachkeeps you more in a "testing mindset".Also for consistency.
7 remaining items
maxsudik commentedon Dec 5, 2023
+1 for
test.eachgiuliagutierrez commentedon Dec 21, 2023
I don't know if there's something wrong with my code, but the setup doesn't work with the
foraproach.If others have the same problem, it would be another valid point to add the
test.eachfeature.jamieomaguire commentedon Jan 2, 2024
I'd also prefer using
test.eachsyntax to the for loop, fwiw. I am finding the loop approach to be rather clunky.vekunz commentedon Jan 4, 2024
test.eachanddescribe.eachwould have an advantage for IDE integrations also. These integrations cannot handle tests inside a regular for-loop, but the*.eachsyntax could be handled well.Morgy93 commentedon Jan 26, 2024
This does not work. I need to put the loop inside the "check link" test. MAYBE or HOPEFULLY, this is approach is possible with
test.each.iamfj commentedon Jan 26, 2024
Could we have an update on the progress of the ticket regarding the
eachsyntax implementation? Other testing frameworks have already adopted this feature, and it would be beneficial for us to understand the timeline for its integration into our processes.PowerCreek commentedon Jan 27, 2024
disclaimer: this is just a gist of how to shape the code setup for what your goals are.
disclaimer2: I quickly whipped this up together, so pick it apart if you want, ya'll can take it and run with it
testProxy.js
sample.spec.js
it's basically cheating.
PowerCreek commentedon Jan 27, 2024
scenario.js
your.spec.js
davidenke commentedon Feb 8, 2024
For the time being we're using a helper tagging function:
This can be used in a for loop with a template string:
curiosity26 commentedon Mar 7, 2024
+1 to
test.eachanddescribe.eachwith parallelization. The ergonomics of playwright already align with things like testing library and jest, so why not complete the catalog? As an engineer, if I can flow between unit tests and e2e tests without having to reach for different tooling, it makes my life easier.AsemK commentedon May 17, 2024
+1 for
test.eachdoomjar commentedon Jun 11, 2024
Another +1 for
test.each/describe.eachmxschmitt commentedon Jun 17, 2024
Hey everyone,
We discussed this feature extensively within the team and concluded that using a
forEachloop is preferable totest.each. Using the standard JavaScript syntax gives you full IDE support. See the discussion in this StackOverflow post for more details.Using
forEachwon't affect parallelism; tests will continue to run in parallel as per our parallelism documentation.Here is an example:
Thank you,
< 3 Playwright Team