Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto-Execute method / code before or after EVERY test method runs

I have inherited a test project with thousands of test methods / classes and I need to implement a new clean up procedure that needs to run after or before every test. I know this is not the best but this is the current situation I am in. In a year from now we will hopefully have all these tests rebuilt properly. I need to some how run a method before or after every test without hard codding the method in every single test class as we just do not have time for this. I know there are attributes for assembly clean up and init, but that only runs before and after the entire assembly. I need something like this, but that runs after every test without coding that functionality per test.

Is this possible? What are some options?

EDIT I am using MSTest

like image 802
Landin Martens Avatar asked Apr 07 '26 03:04

Landin Martens


1 Answers

Assuming you're using MSTest, the attribute you need for the set up method is [TestInitialize]. A method decorated with this attribute will run before every single test.

like image 83
Claudio Redi Avatar answered Apr 09 '26 16:04

Claudio Redi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!