How does GUI.Toggle work?

I have a level editor with a test button that I want to change into a toggle button - I want it to turn on when pressed and stay on until pressed again. Currently I have a setup like this

//In variable declaration
private bool testButtonToggle = false; //Off by default

//In OnGUI()
if (GUI.Toggle(testButtonRect, testButtonToggle, "", testToggleStyle)) {
     Debug.Log("Toggle code block ran: " + testCounter + " times!");
     if (testButtonToggle)
     {
            testButtonToggle = false;
            //Turn off test mode
     }
     else if (!testButtonToggle)
     {
            testButtonToggle = true;
            //Turn on test mode
     }
     testCounter++;
}

With it coded that way, it stays toggled on when you first press it and while you hold down the mouse button, but as soon as the mouse goes up it toggles off instantly. That led me to think that when you give a Toggle a bool parameter it automatically toggles it when it is pressed, but that makes it function like a normal button and it will only run the ‘else if (false)’ block.

Just in addition to Eric’s answer:

A Toggle just switches between two states. It doesn’t have something like “onMouseDown” or “onMouseUp” events. It just turns the variable on when you click it once (and it stays on) and when you click again it turns off again (and stays off)

private bool testButtonToggle = false;

void OnGUI()
{
    testButtonToggle = GUI.Toggle(testButtonRect, testButtonToggle, "", testToggleStyle);
    if (testButtonToggle)
    {
        // Do something when it's toggled on, for example draw addtional GUI elements:
        if (GUI.Button(...))
        {
            //...
        }
    }
}

I have to admit that the example on the Toggle page is terrible.

edit
Just read your comment above. Well The problem is OnGUI is ment for GUI element / event processing and might get called several times per frame. If you want to execute code every frame you should do it like this:

private bool testButtonToggle = false;

void OnGUI()
{
    testButtonToggle = GUI.Toggle(testButtonRect, testButtonToggle, "", testToggleStyle);
}

void Update()
{
    if (testButtonToggle)
    {
        // Do something when it's toggled on
    }
    else
    {
        // Do something else when it's toggled off.
    }
}

edit2:

To detect a state change you just have to store value of your state variable from the last frame and compare it with the current. If it’s different a change has occurred.

private bool testButtonToggle = false;
private bool oldTestButtonToggle = false;

void OnGUI()
{
    // Responsible for the state change
    testButtonToggle = GUI.Toggle(testButtonRect, testButtonToggle, "", testToggleStyle);
    // When the two variables are uneven a change has occurred. Either has been turned
    // on or off this frame
    if (testButtonToggle != oldTestButtonToggle) 
    {
        if (testButtonToggle)
        {
            // on event
        }
        else
        {
            // off event
        }
    }
}

With a toggle you can simplify it like this:

private bool testButtonToggle = false;

void OnGUI()
{
    if (GUI.Toggle(testButtonRect, testButtonToggle, "", testToggleStyle) != testButtonToggle)
    {
        testButtonToggle = !testButtonToggle; // toggle manually
        if (testButtonToggle)
        {
            // on event
        }
        else
        {
            // off event
        }
    }
}

Now the toggle works just like a normal button. The only difference is that you have an active state which changes it’s visual appearence.

Please see the example in the docs; you assign the result of GUI.Toggle to a variable.