locked
Unit test, C#, WPF application, WinForms RRS feed

  • Question

  •    

    Dear all.

    I have an application with a web server.
    Unfortunately, I have to prepare everything without a remote station.
    I call this function. GetRequest.
    How can I now call this function, which returns me a test object.
      What do I have to do exactly, a switch I will need.
      Compiler switch?

       Switch to test or right function, how can i make it ?
      Process flow

    static void Main(string[] args)
            {          
                ToDo1();
                ToDo2();
                ToDo3();
                GetRequest(); // Call an external function, at moment I do not have it.
                ToDo5();
                ToDo6();

    This guide is not clear to me.

    //Maybe someone knows a better, a video.
      
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    
    public async Task GetRequest(string ipPort, string webRequest, int timeoutSeconds)
    {
    //..
    using (var response = await client.GetAsync(webRequest))
    {
    	try
    	{
    		// await Task.Delay(250);
    
    		HttpResponseMessage ret = response.EnsureSuccessStatusCode();
    		// If we reach here so it means we can get the AssemblyNumber data.
    		report = await response.Content.ReadAsAsync<AssemblyNumber>();
    	}
    	catch (HttpRequestException)
                            {
                                if (response.StatusCode == System.Net.HttpStatusCode.NotFound) // 404
    							
    				

    With best regards Markus


    
    • Moved by Fei Hu Friday, January 26, 2018 8:04 AM Unit Testing related
    Thursday, January 25, 2018 5:07 PM

Answers

  • Hi Markus,

    >> Switch to test or right function, how can i make it?

    Like you said, you need a compiler switch.

    static void Main(string[] args)
    {          
        ToDo1();
        ToDo2();
        ToDo3();
    #if REMOTE_SERVER_ISOK
        GetRequest(); // Call an external function, at moment I do not have it.
    #endif
        ToDo5();
        ToDo6();
    }

    Please right click your Unit Test Project, choose Properties.

    please add "REMOTE_SERVER_ISOK" in "Conditional compilation symbols:" enter box under Build tab when GetRequest() function should be called, and remove it when that function should not be called.

    Please refer to following article for more details.

    https://docs.microsoft.com/en-us/dotnet/framework/debug-trace-profile/how-to-compile-conditionally-with-trace-and-debug

    Regards,

    Judyzh

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.




    • Edited by Judy ZhuY Monday, January 29, 2018 11:22 AM
    • Proposed as answer by Judy ZhuY Tuesday, January 30, 2018 1:34 AM
    • Marked as answer by Markus Freitag Tuesday, January 30, 2018 5:37 PM
    Monday, January 29, 2018 11:18 AM

All replies

  • The logic for testing anything is to have a unit test project that has test methods to test your app methods and your test methods need to work with test cases that would happen both good and bad so they are repeatable test. The basics are here. By default test methods are of type void, when doing async test change test method to be of type Task.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, January 25, 2018 6:25 PM
  • From what you have shown, you will not doing a unit test, because you are using a network.

    http://www.artima.com/weblogs/viewpost.jsp?thread=126923

    Thursday, January 25, 2018 6:57 PM
  • Hi Markus,

    Your issues is more related to unit test, I will move the thread to unit testing forum for suitable support.

    If you have some grammar or code errors in using C#, please feel free to contact us. We will try our best to give you a solution.

    Best Regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, January 26, 2018 8:03 AM
  • Hello,
    my problem is.
    MainApp.EXE to run with Unittest, returns default values.
         Deliver to customer
    MainApp.EXE to run with server from customer, returns customer values.
    Where I can define that?
    Is there a way to have a test report printed?
    Main.App
    Step1()
    Step2_toServer()   // I haven't the server
    Step3()
    Step4()

    Or maybe it is.
    Inside the unit test, I can't test my whole application. I can only test small units, assemblies.
    That is it. Right?

    If so, what do the experts recommend? Inside Step2_toServer() return dummy values?

    Regards Markus

    I  found this, but is not all. I looking for a good tutorial

    Friday, January 26, 2018 5:14 PM
  • Hi Markus,

    >> Switch to test or right function, how can i make it?

    Like you said, you need a compiler switch.

    static void Main(string[] args)
    {          
        ToDo1();
        ToDo2();
        ToDo3();
    #if REMOTE_SERVER_ISOK
        GetRequest(); // Call an external function, at moment I do not have it.
    #endif
        ToDo5();
        ToDo6();
    }

    Please right click your Unit Test Project, choose Properties.

    please add "REMOTE_SERVER_ISOK" in "Conditional compilation symbols:" enter box under Build tab when GetRequest() function should be called, and remove it when that function should not be called.

    Please refer to following article for more details.

    https://docs.microsoft.com/en-us/dotnet/framework/debug-trace-profile/how-to-compile-conditionally-with-trace-and-debug

    Regards,

    Judyzh

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.




    • Edited by Judy ZhuY Monday, January 29, 2018 11:22 AM
    • Proposed as answer by Judy ZhuY Tuesday, January 30, 2018 1:34 AM
    • Marked as answer by Markus Freitag Tuesday, January 30, 2018 5:37 PM
    Monday, January 29, 2018 11:18 AM