c# - unit Test

Dibara 1 Reputation point
2021-05-31T17:52:54.513+00:00

Dear Devlopers:

it is my first time that i do a unit test,
here is th method that i wanna do a test on it.

class MainWinow

private void Start_Click(object sender, RoutedEventArgs e)
{
if (_timer > 0)
{
DeactivateButtons();
_board.NextGeneration();
_timerRef = _board.PlayWithTimer(_timer);
}
else
{
ButtonCancel.IsEnabled = true;
_board.NextGeneration();
}
}

how can i do this test?

thanks.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,936 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,421 Reputation points
    2021-05-31T18:48:03.503+00:00

    If one or more variables, methods or properties are privately scoped you should focus your test methods on publicly scoped items. And never change scope to accommodate testing.

    Now if classes available can be created in a test project, this is what you do along with setting up _timer for one test as 0, another greater than 0.

    In regards to DeactivateButtons, assuming this changes say enabled aspect of one or more controls, that can be done without a test method. Simply place a button on the window with only DeactivateButtons and visually see if it works.

    Personally, I create a test class then create a folder named base, add another class, same name as the first. Both must have the same namespace and marked as partial, place any code needed to initialize test there.

    Add a method as per below which prepares specific test methods if needed.

    [TestInitialize]
    public void Init()
    {
        if (TestContext.TestName != nameof(YourTestName)) return;   
    
    }
    

    To get an idea of structing a unit test project.

    0 comments No comments

  2. Lex Li (Microsoft) 5,582 Reputation points Microsoft Employee
    2021-05-31T19:49:24.243+00:00

    What you want to do is more than the traditional "unit testing", but UI test automation.

    That requires tools like WinAppDriver or other alternatives like FlaUI,

    https://github.com/microsoft/WinAppDriver/issues/1371

    0 comments No comments

  3. Rocky 1 Reputation point
    2021-07-14T04:39:41.32+00:00

    Hi team,

    Is it possible to write unit test and run in editor i think it is not possible since it required to add few packages

    any suggestion


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.