Jump to content

Question for the best approach to write code in multiple test.


Go to solution Solved by Zedna,

Recommended Posts

Posted (edited)

I am writing scripts to test four communications... TCP, Serial, USB, and Optical. Let's say that I have 5 test cases:

TC#1 is communicating TCP

TC#2 is do A

TC#3 is do B

TC#4 is do C

TC#5 is do D

Next test case 1 is communicating via Serial 485 now since TCP just finished testing.

Then TC#2 to #5 will be used again but not in TCP but in serial 485 now which is identical test.

Same goes to USB and Optical ports.. 

So basically, the test case #1 is the only different to the steps while test #2 to 5 will be reused to all comms (tcp, serial, usb, optical)

What would be the best approach to code this so the line of code will be minimized?

I want to reduce the redundant use of test cases 2 to 5 since those test are the same to all comms. 

Any help and critics are welcome.

Here are my sample scripts:

#include <GUIConstantsEx.au3>
#include <AutoItConstants.au3>
Global $comms
$commsTCP = "TCP"
$comms485 = "485"
$commsUSB = "USB"
$commsOptics = "Optical"


Example()

Func Example()
    ; Create a GUI with various controls.
    Local $hGUI = GUICreate("Example", 300, 200)

    ; Create a button control.
    Local $idButton_runtest = GUICtrlCreateButton("Run Test", 120, 170, 85, 25)
    Local $idButton_Close = GUICtrlCreateButton("Close", 210, 170, 85, 25)

    ; Display the GUI.
    GUISetState(@SW_SHOW, $hGUI)

    Local $iPID = 0

    ; Loop until the user exits.
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE, $idButton_Close
                ExitLoop

            Case $idButton_runtest

               ;TCP
               test1($commsTCP)
               test2()
               test3()
               test4()
               test5()

               ;Serial 485
               test1($comms485)
               test2()
               test3()
               test4()
               test5()

               ;USB
               test1($commsUSB)
               test2()
               test3()
               test4()
               test5()

               ;Optical
               test1($commsOptics)
               test2()
               test3()
               test4()
               test5()

        EndSwitch
    WEnd

    ; Delete the previous GUI and all controls.
    GUIDelete($hGUI)

    ; Close the Notepad process using the PID returned by Run.
    If $iPID Then ProcessClose($iPID)
    EndFunc   ;==>Example

Func test1($comms)
   SplashTextOn("", "Communicating via " & $comms, 280, 60, -1, -1, $DLG_CENTERONTOP , "", 14)
   Sleep(1000)
   SplashOff()
EndFunc
Func test2()
   SplashTextOn("", "Test Case 2", 280, 60, -1, -1, $DLG_CENTERONTOP , "", 14)
   Sleep(1000)
   SplashOff()
EndFunc
Func test3()
   SplashTextOn("", "Test Case 3", 280, 60, -1, -1, $DLG_CENTERONTOP , "", 14)
   Sleep(1000)
   SplashOff()
EndFunc
Func test4()
   SplashTextOn("", "Test Case 4", 280, 60, -1, -1, $DLG_CENTERONTOP , "", 14)
   Sleep(1000)
   SplashOff()
EndFunc
Func test5()
   SplashTextOn("", "Test Case 5", 280, 60, -1, -1, $DLG_CENTERONTOP , "", 14)
   Sleep(1000)
   SplashOff()
EndFunc

I'm somewhat new to programming and just barely manage to write something good (so far).

 

Edited by Melba23
Added code tags
Posted (edited)

I did this to manage to minimize the line of codes.. but I am somewhat not satisfied since what I made is kinda silly.

#include <GUIConstantsEx.au3>
#include <AutoItConstants.au3>
Global $comms
$commsTCP = "TCP"
$comms485 = "485"
$commsUSB = "USB"
$commsOptics = "Optical"


Example()

Func Example()
    ; Create a GUI with various controls.
    Local $hGUI = GUICreate("Example", 300, 200)

    ; Create a button control.
    Local $idButton_runtest = GUICtrlCreateButton("Run Test", 120, 170, 85, 25)
    Local $idButton_Close = GUICtrlCreateButton("Close", 210, 170, 85, 25)

    ; Display the GUI.
    GUISetState(@SW_SHOW, $hGUI)

    Local $iPID = 0

    ; Loop until the user exits.
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE, $idButton_Close
                ExitLoop

            Case $idButton_runtest

               ;TCP
               test1($commsTCP)
               all()

               ;Serial 485
               test1($comms485)
               all()

               ;USB
               test1($commsUSB)
               all()

               ;Optical
               test1($commsOptics)
               all()

        EndSwitch
    WEnd

    ; Delete the previous GUI and all controls.
    GUIDelete($hGUI)

    ; Close the Notepad process using the PID returned by Run.
    If $iPID Then ProcessClose($iPID)
    EndFunc   ;==>Example

Func all()
   test2()
   test3()
   test4()
   test5()
EndFunc

Func test1($comms)
   SplashTextOn("", "Communicating via " & $comms, 280, 60, -1, -1, $DLG_CENTERONTOP , "", 14)
   Sleep(1000)
   SplashOff()
EndFunc
Func test2()
   SplashTextOn("", "Test Case 2", 280, 60, -1, -1, $DLG_CENTERONTOP , "", 14)
   Sleep(1000)
   SplashOff()
EndFunc
Func test3()
   SplashTextOn("", "Test Case 3", 280, 60, -1, -1, $DLG_CENTERONTOP , "", 14)
   Sleep(1000)
   SplashOff()
EndFunc
Func test4()
   SplashTextOn("", "Test Case 4", 280, 60, -1, -1, $DLG_CENTERONTOP , "", 14)
   Sleep(1000)
   SplashOff()
EndFunc
Func test5()
   SplashTextOn("", "Test Case 5", 280, 60, -1, -1, $DLG_CENTERONTOP , "", 14)
   Sleep(1000)
   SplashOff()
EndFunc


 

Edited by Melba23
Added code tags
  • Moderators
Posted

It_is_me_Me,

Welcome to the AutoIt forums. When you post code in future please use Code tags - see here how to do it.  Then you get a scrolling box and syntax colouring as you can see above now I have added the tags. Thanks in advance for your cooperation.

M23

 

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

  • Solution
Posted (edited)
#include <GUIConstantsEx.au3>
#include <AutoItConstants.au3>

Global $comms
$commsTCP = "TCP"
$comms485 = "485"
$commsUSB = "USB"
$commsOptics = "Optical"

Example()

Func Example()
    ; Create a GUI with various controls.
    Local $hGUI = GUICreate("Example", 300, 200)

    ; Create a button control.
    Local $idButton_runtest = GUICtrlCreateButton("Run Test", 120, 170, 85, 25)
    Local $idButton_Close = GUICtrlCreateButton("Close", 210, 170, 85, 25)

    ; Display the GUI.
    GUISetState(@SW_SHOW, $hGUI)

    Local $iPID = 0

    ; Loop until the user exits.
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE, $idButton_Close
                ExitLoop

            Case $idButton_runtest
               test1($commsTCP) ;TCP
               test1($comms485) ;Serial 485
               test1($commsUSB) ;USB
               test1($commsOptics) ;Optical

        EndSwitch
    WEnd

    ; Delete the previous GUI and all controls.
    GUIDelete($hGUI)

    ; Close the Notepad process using the PID returned by Run.
    If $iPID Then ProcessClose($iPID)
    EndFunc   ;==>Example

Func test_2to5()
   test2()
   test3()
   test4()
   test5()
EndFunc

Func test1($comms)
   SplashTextOn("", "Communicating via " & $comms, 280, 60, -1, -1, $DLG_CENTERONTOP , "", 14)
   Sleep(1000)
   test_2to5()
   SplashOff()
EndFunc

Func test2()
   SplashTextOn("", "Test Case 2", 280, 60, -1, -1, $DLG_CENTERONTOP , "", 14)
   Sleep(1000)
EndFunc
Func test3()
   SplashTextOn("", "Test Case 3", 280, 60, -1, -1, $DLG_CENTERONTOP , "", 14)
   Sleep(1000)
EndFunc
Func test4()
   SplashTextOn("", "Test Case 4", 280, 60, -1, -1, $DLG_CENTERONTOP , "", 14)
   Sleep(1000)
EndFunc
Func test5()
   SplashTextOn("", "Test Case 5", 280, 60, -1, -1, $DLG_CENTERONTOP , "", 14)
   Sleep(1000)
EndFunc

 

Edited by Zedna

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...