MSTest: how to increase test time
Asked Answered
M

3

25

I have one test that needs to work more then 1 minute (VS2008, MSTest, tests are launched from the VisualStudio):

    const int TestTimeout = 1;

    [TestMethod]
    [Timeout(10*60*1000)] // 10 minutes
    public void Login_ExpirationFail_Test()
    {
        IAuthenticationParameters parameters = new AuthenticationParameters(...);
        LdapAuthentication auth1 = new LdapAuthentication();
        IAuthenticationLoginResult res = auth1.Login(parameters);

        Assert.IsNotNull(res);
        Assert.IsFalse(string.IsNullOrEmpty(res.SessionId));

        const int AdditionalMilisecodns = 400;
        System.Threading.Thread.Sleep((TestTimeout * 1000 + AdditionalMilisecodns) * 60);

        LdapAuthentication auth2 = new LdapAuthentication();
        auth2.CheckTicket(res.SessionId);
    }

This test is finished in "Run" mode with "Test 'Login_ExpirationFail_Test' exceeded execution timeout period." error message, in "Debug" - it works fine.

I saw few similar problems linked to launching tests from the command line.

How could I get my test workable in "Run" mode?

Thanks.

Monkfish answered 5/11, 2010 at 20:27 Comment(0)
M
20

Answer is very simple: attribute value should be a constant, not an expression.

Change

[Timeout(10*60*1000)]

to

[Timeout(600000)]

resolved an issue.

EDIT: Comment to the answer brought to my attention a mistake I've done originally in the answer (wrote "60000" as timeout value). In my source code I have 6000000 and that value helped. the answer was corrected recently

Monkfish answered 10/11, 2010 at 16:52 Comment(4)
Your math is wrong. You've dropped it by a factor of 10. Furthermore, the expression vs. constant above are exactly equivalent because the C# compiler will turn it to the same thing (except for the math error). If this solution worked, I suspect it's because the math error brought the value down to something within the range that mstest requires.Spriggs
Hey Andrew, thank you for brining this to our attention. That was just a misprint in the answer. I've used the proper math in my source code. I don't know why that helped, but that did.Monkfish
@Monkfish How is this an answer? Why is, it should be a constant, an answer? A constant expression is just that, constant and as Andrew pointed out this is what the compiler does anyway. The only thing you manage to accomplish by turning this into a constant yourself is a lot of zeroes that can be easily misread as I think this answer itself can attest to, the expression is fine and much more readable.Clairvoyant
Uh... err... "10*60*1000" IS a constant (a constant expression). This works in VS 2010. Did this change from VS 2008?Feodor
A
13

In addition to specifying the number of seconds, Timeout() supports a constant that allows for infinite waiting.

[Timeout(TestTimeout.Infinite)]
Alerion answered 22/2, 2016 at 23:22 Comment(4)
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From ReviewCards
The original question was a problem with the test timing out, for example in my case, my test ran for 3h+ so the correct answer was the first answer, to set the time, my answer said "can also do" which means, if the developer have a long running test and does not know how long it takes, TestTimeout.Infinte is the best option. Please elaborate why this is not a good answer?Alerion
It is best if answers explain their own rationale and don't rely on other answers for context, but I agree that this is a helpful addition. Apologies for the review process being sometimes a bit confusing without context.Cards
this looks like a really.. really.. bad idea :)Bratcher
A
0

Not specific steps, but should point you in the right direction:

Add a test settings file to the solution if you don't already have one.

Open the configuration wizard with the test settings, and look for the setting that controls the test timeout.

Armoire answered 5/11, 2010 at 20:49 Comment(3)
There are such settings in the tests configurations, but they are related to all tests... while I want to have timeout extended for 1 test only...Monkfish
why are you applying Timeout to the const? I haven't used that attribute, but it surely should be applied to the test methodArmoire
Sorry for misprint. Definitely, [Timeout] was applied to method. Here was a misprint (recently corrected).Monkfish

© 2022 - 2024 — McMap. All rights reserved.