Pureunit tutorial

Just starting out? Need help? Post your questions and find answers here.
Reku
New User
New User
Posts: 2
Joined: Thu Oct 14, 2021 5:11 pm

Pureunit tutorial

Post by Reku »

It appears that I can't really get pure unit to work and give consistent results.

I have the resource file in my PB folder and I linked the pure unit tool to the menu structure.

Say I have the following code in 2 files demoMain.pb and DemoTest.pb :

Code: Select all

Global var = 1

Procedure Go()
  OpenConsole()
  PrintN("test")
  Delay(2000)
  var = 4
EndProcedure

Go()
Delay(10000)
and the test file:

Code: Select all

XIncludeFile "demoMain.pb"

PureUnitOptions(NoUnicode, SubSystem[OpenGl])

    ProcedureUnit TestGo()
    Assert(var = 4)
    EndProcedureUnit
and then I have a project with just the first file in it.

when I run pure unit sometimes it fails and other times it passes and sometimes it almost seems like it doesn't detect changes in the first file.

for example global var will be set to 1 outside the go() procedure and then var will be 4 inside the go() procedure which would change the value outside of the go() procedure to 4 but then in the unit test file I'm stating that the var needs to be equal to 4. It will fail the unit test. I can confirm that the value is changing in the variable viewer debug tool.

I am probably not using pureunit correctly - is there another unit testing tool that works better or is there some documentation that I am missing - I've only read 1 forum post able weird behavior in pureunit from 2019 and the readme that comes with it in the directory.

Is there anywhere I can view some pureunit demo code that is more than the bit of code in the readme. Or do purebasic programs just not get extensively tested using pureunit. Am I just better off not unit testing using this language?

Thanks - sorry for the lengthy first post.
User avatar
spikey
Enthusiast
Enthusiast
Posts: 581
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: Pureunit tutorial

Post by spikey »

Something like this:

Code: Select all

; MainCode.pb File
Global UpVar, DownVar

Procedure Up(Value.i)
  UpVar + Value
EndProcedure

Procedure Down(Value.i)
  DownVar - Value
EndProcedure

; Unit Test File
XIncludeFile "MainCode.pb"

PureUnitOptions(NoUnicode, SubSystem[OpenGl])

ProcedureUnitStartup InitialiseUpVar()
UpVar = 1
EndProcedureUnit

ProcedureUnitStartup InitialiseDownVar()
DownVar = 100
EndProcedureUnit

ProcedureUnit TestUp()
Up(1)
Assert(UpVar = 2)
Up(2)
Assert(UpVar = 4)
Up(3)
Assert(UpVar = 7)
EndProcedureUnit

ProcedureUnit TestDown()
Down(1)
Assert(DownVar = 99)
Down(2)
Assert(DownVar = 97)
Down(3)
Assert(DownVar = 94)
EndProcedureUnit
1) The unit tester won't execute code outside of a procedure. So you can't do variable initialisation in the main scope like this:-

Code: Select all

Global var = 1  ; This won't work...
That's what the ProcedureUnitStartup procedure(s) are for.

2) The test procedures should perform tests with predictable results that can be verified using an Assert(), or other testing and Fail().
In this case I set the value of 'UpVar' and 'DownVar' in the Initialise...() procedures (I didn't really need two here of course).
I then call Up() and Down() with known arguments several times in a test procedure and assert that the results match my expectations afterwards.
You need to call the normal procedures to be tested from inside a ProcedureUnit procedure or they won't get tested.

3) Bear in mind that where there are multiple test procedures they will be executed in random order.
Your test procedures must take this into account and not assume that processing will always follow the same sequence.

4) The tests are supposed to be hands-free so you shouldn't rely on UI elements for testing, use UnitDebug to put messages in the log/report.
I don't know if anyone else's experience is the same but PureUnit won't even open a console window on my pc...
Reku
New User
New User
Posts: 2
Joined: Thu Oct 14, 2021 5:11 pm

Re: Pureunit tutorial

Post by Reku »

Thanks! I will try out your suggestions and see how it goes.
User avatar
C87
Enthusiast
Enthusiast
Posts: 176
Joined: Mon Jul 17, 2017 7:22 am
Location: Cotswolds England

Re: Pureunit tutorial

Post by C87 »

I had no idea that PureUnit even existed :oops: Having found it I see there are dozens of libraries, which I also didn't know about.
Forgive my ignorance I'm just a database system guy, but which ASCII compiler could anyone suggest to create the .EXE as shown by Keya at
viewtopic.php?f=12&t=63444
If it's falling over......just remember the computer is never wrong!
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Pureunit tutorial

Post by infratec »

@C87

I made the necesarry modifications for the Pure Library Explorer:

viewtopic.php?p=576316#p576316
User avatar
C87
Enthusiast
Enthusiast
Posts: 176
Joined: Mon Jul 17, 2017 7:22 am
Location: Cotswolds England

Re: Pureunit tutorial

Post by C87 »

A quick note to thank you Infratec. It works brilliantly, thank you so much.
On my PC it is called InfratecLibsView.exe
If it's falling over......just remember the computer is never wrong!
Post Reply