-
Notifications
You must be signed in to change notification settings - Fork 261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Class Initialize inheritance #143
Comments
I believe this works if the test class and base class are in the same assemblies. I'm curious if issue #23 fixed this behavior. |
This works for |
That sounds great! Though it would be fairly easy to work-around for now, since your test class [ClassInitialize] static method could just manually call your base class [ClassInitialize] static method. Using a static constructor would also be a work around. Would love to see this "bug" fixed though! I've been working on a lot of unit tests lately that could really use this pattern. |
Hello |
I'm very much pro this idea!
Therefore I suggest to add an optional enum parameter to the ClassInitializeAttribute with the following possible values:
Please let me know what you think. |
Thanks for detailing that out @arnonax . That does sound interesting although I was hoping to just stick with |
Could this work for |
Of course. ClassCleanup should be symmetrical to ClassIntialize. |
Any news? |
Not sure if there's anyone working on this issue, but I've assumed that no one is working on it and grab this one. I've started to implement this new enhancement, and so far so good. Added the parameter to the let me know your thoughts. |
@parrainc |
@mprice-hw that is correct. ClassCleanup should be symmetrical to ClassInitialize.
Notice that Init method from Anyone please, feel free to review/clone/fork my repo parrainc/testfx if you want to try this out, or maybe help me with anything i've missed. @AbhitejJohn please advice if, in case this changes are approved, we'll be sticking to BeforeEach and None options, or also including the BeforeAny option. That way, I could focus more on polishing the first two, instead of the three ones (in case of not being necessary). |
Any update? Also does ClassInitialize and ClassCleanup work with Async i.e. Task results? Thanks. |
@parrainc : That sounds fantastic, I'm super late to this though but I think we could definitely start with just that two. I've moved on to other things but @jayaranigarg and @singhsarab could help if you still need any. @AceHack : From here ClassInitialize and ClassCleanup should be async Task friendly too. |
@AbhitejJohn cool, It's been a while since I opened the testfx project on my computer, so I have to get my fork up-to-date. After that, will be submitting the PR so @jayaranigarg and @singhsarab can take a look when available. |
* Implemented Initialize Inheritance for ClassInitialize attribute * Implemented Cleanup inheritance behavior for ClassCleanup attr * Removed OnceBeforeAnyDerivedClasses from ClassInitializeInheritance enum * Changed UTF.TestClass for custom testclass attr to avoid test ouput warnings * Updated RunClassCleanup to prevent running base cleanup without having run base init * Fix name typo * added doc for BaseClassInitializeMethodsDict prop * Updates per review comments * making inheritance behavior enum more generic and updating tests * done some refactoring and added/fixed a few tests * updates as per review comments * updated classinfo and tests * updated classcleanup 1-param ctor * updating doc message for 1-param ctors
Consume this change using following nuget packet versions: |
This issue is still occuring for me, which is very weird because yesterday it seemed just fine. `
}
The assembly is being called, the test method is being called. INFORMATION: [2019-08-09 08:57:23] AssemblyInitialize: 12 [BaseTest.cs > Assembly() > Line 21] ClassInitialize and Cleanup are not being called |
@KitoCoding : For compat reasons this is an opt in at the moment. You'd need to add |
@AbhiteJohn |
@KitoCoding : you'd need the packages from the daily feed Jaya posted earlier. It doesn't look like this is part of the public nuget feed just yet. |
@AbhiteJohn, this Nuget package is missing. I have tried with the different builds, but it is not able to find any. |
@KitoCoding I was able to get the changes from version 2.0.0 in the daily feed. And your example code worked with no issues. |
Not sure if this was a design choice or a bug, but for this simple scenario, Two calls is better than none, but it does complicate things if we have to track these extra calls in order to prevent test data from cleaning up before all of the tests are done. using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace SomeTests
{
[TestClass()]
public abstract class TestCase
{
[ClassInitialize(InheritanceBehavior.BeforeEachDerivedClass)]
public static void InitMe(TestContext context)
{
}
[TestMethod]
public void TestFromBase()
{
}
[ClassCleanup(InheritanceBehavior.BeforeEachDerivedClass)]
public static void CleanMe()
{
}
}
[TestClass()]
public class DerivedTestCase : TestCase
{
[TestMethod]
public void TestFromDerived()
{
}
}
} In addition, |
I found another issue: When the derived type is in another assembly that is referenced in the same project, the Since I am developing a test case framework for an application for 3rd parties to inherit, I have no way of knowing what the assembly name might be, and it wouldn't be very efficient for me to have to load all referenced assemblies to find it. Also, since my goal is to look for one of my custom attributes on the 3rd party test class, it seems like it might be a good idea to add the |
Added PR to fix issue where base class cleanup isn't called. |
Are there any plans to fix the From my (very) simple tests, like other have already mentioned, it looks like the <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.0" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.0" /> TestBase.cs using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace TestFxIssue143
{
[TestClass]
public class TestBase
{
public TestContext TestContext { get; set; }
[ClassInitialize(InheritanceBehavior.BeforeEachDerivedClass)]
public static void ClassInitialize(TestContext context)
{
Console.WriteLine($"[{DateTime.Now.ToString("HH:mm:ss.fff")}] ClassInitialize: {context.FullyQualifiedTestClassName}/{context.TestName}");
}
[ClassCleanup(InheritanceBehavior.BeforeEachDerivedClass)]
public static void ClassCleanup()
{
Console.WriteLine($"[{DateTime.Now.ToString("HH:mm:ss.fff")}] ClassCleanup");
}
[TestInitialize()]
public void TestInitialize()
{
Console.WriteLine($"[{DateTime.Now.ToString("HH:mm:ss.fff")}] TestIntialize: {this.TestContext.FullyQualifiedTestClassName}/{this.TestContext.TestName}");
}
[TestCleanup()]
public void TestCleanup()
{
Console.WriteLine($"[{DateTime.Now.ToString("HH:mm:ss.fff")}] TestCleanup: {this.TestContext.FullyQualifiedTestClassName}/{this.TestContext.TestName}");
}
}
} UnitTest1.cs using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace TestFxIssue143
{
[TestClass]
public class UnitTest1 : TestBase
{
[TestMethod]
public void TestMethod1()
{
Console.WriteLine($"[{DateTime.Now.ToString("HH:mm:ss.fff")}] TestMethod: {this.TestContext.FullyQualifiedTestClassName}/{this.TestContext.TestName}");
}
[TestMethod]
public void TestMethod2()
{
Console.WriteLine($"[{DateTime.Now.ToString("HH:mm:ss.fff")}] TestMethod: {this.TestContext.FullyQualifiedTestClassName}/{this.TestContext.TestName}");
}
[TestMethod]
public void TestMethod3()
{
Console.WriteLine($"[{DateTime.Now.ToString("HH:mm:ss.fff")}] TestMethod: {this.TestContext.FullyQualifiedTestClassName}/{this.TestContext.TestName}");
}
}
} And the output is:
TestMethod2:
TestMethod3:
I noticed the |
@maboivin there are not plans to fix this at the moment, for the next version we are focusing on stabilization, performance and low-hanging fruit, which this unfortunately is not, but this issue has multiple upvotes and we go from the most upvoted / most commented when looking at issues, so hopefully soon. |
Good news. Thanks. I will take a look when the next release is available. |
I have the following in my base class:
And it's not being executed for the classes inherting from the base class. Shouldn't it work? |
@poserdonut yes that should work. Are you using the latest version that has these changes? How did you confirm it didn't get executed? Did you try in a debugger? Note that your text in your call to WriteLine won't end up getting logged anywhere. |
Latest version (not preview) and we were using debug. Had to switch to testcleanup instead. |
Same here :( |
Any luck so far? This is really not working as it should |
any update ? |
I am confused as there seems to be two different "things" (one feature request and one bug) being discussed here. As far as I can see, the inheritance of the class initialize is implemented and is working properly (we recently added a bunch of integration tests for this: https://github.com/microsoft/testfx/blob/2d2ab810a78fa98855a9e56cb5a18d7f963c45f2/test/IntegrationTests/MSTest.VstestConsoleWrapper.IntegrationTests/SuiteLifeCycleTests.cs). @enisak Could you please let us know which version of MSTest you are using? Also, would you mind providing a small repro of what's not working so we can investigate (cc @engyebrahim)? |
The feature request was completed. There were some bugs with it that I fixed but AFIK there aren't any other issues. The feature is working fine for my team. Maybe this should get closed out an bugs be opened if anyone has specific issues. |
Agreed, let me do that now. |
Description
Allow a ClassInitialize in a base class of a TestClass so that in addition to initializing static variables in the test class, I can initialize static variables in the test base class.
UV item: https://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/2311366-support-classinitialize-in-base-class
The text was updated successfully, but these errors were encountered: