Unit tests

peacemaker

Expert
Licensed User
Longtime User
HI, All

I know about this term only theoretically.
How should it look in B4X development ? Especially in B4R ...
Any example of codes ?
 

tchart

Well-Known Member
Licensed User
Longtime User
HI, All

I know about this term only theoretically.
How should it look in B4X development ? Especially in B4R ...
Any example of codes ?
Unit tests test exactly that ie a “unit of code”. Generally you would have a separate project that would reference a module from your main project and then test each Sub with an input and then validate that input. You would also have tests that test with a bad input and validate that the error is handled it correctly.

If you look on GitHub at most Java repos under the src directory there will be a main project and then a test project that will call various functions in the main project.

How that translates to B4X I have not yet figured out a good way to do it.
 

peacemaker

Expert
Licensed User
Longtime User
not yet figured out a good way
I did not see, and also cannot imagine yet. Do you have an example of such Java repo to check ?
What feature of the programming language and\or IDE must be to allow creation of such tests ?
 

tchart

Well-Known Member
Licensed User
Longtime User
Ok so have a look at this library


Notice how there are two folders under src?

These are two Java projects; one is the actual library source code and the other are the tests.

If you look at the test directories you’ll see it will test functions in the main source.

In terms of technology you use the same as you are developing in, so B4J for B4J projects. It’s manual to create the unit tests. I don’t think I’ve seen any scripts to create unit tests automatically but I could be wrong there.
 

aeric

Expert
Licensed User
Longtime User
From my understanding, some programming languages use a function call assert to test the functions.
Not sure this practice is useful in RAD tool like B4X or it can be a topic for debate.
Maybe we can test the inputs like division by zero, input Double value where Integer is expected or input String value to an input that expects numeric and input a very long String to test the limit in database field.
 

peacemaker

Expert
Licensed User
Longtime User

Indeed, here always used try\catch and "assertEquals" function:
Java:
        /**
         * Test for
         * {@link Thumbnailator#createThumbnail(File, File, int, int)}
         * where,
         * <p>
         * 1) Input File does not exist.
         * <p>
         * Expected outcome is,
         * <p>
         * 1) Processing will stop with an IOException.
         *
         * @throws IOException
         */
        @Test
        public void testCreateThumbnail_FFII_nonExistentInputFile() throws IOException {
            /*
             * Actual test
             */
            File inputFile = new File("foo.jpg");
            File outputFile = new File("bar.jpg");

            try {
                Thumbnailator.createThumbnail(inputFile, outputFile, 50, 50);
                fail();
            } catch (IOException e) {
                assertEquals("Input file does not exist.", e.getMessage());
            }
        }

topic for debate
Yes, exact for this the topic has been created. To understand the subject...
 

peacemaker

Expert
Licensed User
Longtime User
But ... i don't understand yet what this test tests..., when executed...
File names are ... foolish, it's clear that won't be found.
 

peacemaker

Expert
Licensed User
Longtime User
B4X:
function assert(condition)
{
  if (!condition) {
    throw "Assertion failed! See stack trace for details";
  }
}


Maybe these can be combined for usage ?
But how to use correctly ?
 

peacemaker

Expert
Licensed User
Longtime User

Seems, clear how to use... What is "@test" ?
 

Daestrum

Expert
Licensed User
Longtime User
In B4J, the @test annotation in Java is a bit like using #If DEBUG.

  • In a DEBUG compile, all the code within the #If DEBUG block is compiled and executed.
  • In a RELEASE build, all the code within the #If DEBUG block is excluded from compilation (though it remains in the source file).
However, the key difference with unit tests is that they provide a detailed report indicating the outcome of each test, specifying whether it passed or failed.
 

aeric

Expert
Licensed User
Longtime User
What does it mean ?
When and where are the unit tests useful ? No use in B4X ?
I think it is useful if your app has many forms, inputs or calculations such as Web API and accounting.
I am not sure, I don't write such tests except for if I remember correctly I did it during college.
 

peacemaker

Expert
Licensed User
Longtime User
I have touched the very sensitive area where the software stability is strictly important.
Read about such test, as important subject for such software, but ... do not understand well how and where these tests help to make software more durable.
And see that B4X is not using them at all...
So, maybe impossible, or non-needed for some reasons...

If to understand the root of the subject - maybe it will help to pass it by and do not use as usual...
 

Daestrum

Expert
Licensed User
Longtime User
Super simple example how to emulate @test

B4X:
Sub AppStart (Args() As String)
    Dim a As Int = 3
#if DEBUG    
    ' check against known value
    assertEquals(a,3)
#end if    
    Log("Value = " & a)
End Sub
#if DEBUG
Sub assertEquals(o As Object,testValue As Object)
    If o <> testValue Then
        Log($"Test failed : object ${o} <> ${testValue}"$)
    Else
        Log($"Test passed : object ${o} = ${testValue}"$)
    End If
End Sub
#End If

In DEBUG the test is run - in RELEASE the test is omitted
 

aeric

Expert
Licensed User
Longtime User
I think for B4A, there is Firebase Analytics that report errors from the user devices.
It is also easy to debug desktop app.
For my server app I always write or insert access log data and error logs for unexpected error into database table.

In today development which is most on web, it is normal for developer to test the back-end API since it is consumed by front-end (client) on another device.

In Computer Science, we learned about Software Development Life Cycle, where we have SIT, UAT, regression test, stress test and etc.

In enterprise, developer will be blame if the code is not well reviewed and tested. I think it is more on the project management.
There are software testers and QA that check for bugs before going for production. There are time and money to invest for this work.

For small projects, we just quickly fix production bugs where they are found.

Unit test or any test is just to reduce the concern of business people. It cannot 100% guarantee the software is 100% free of bugs.
Just my 2 cents.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…