Giter Site home page Giter Site logo

accesscodelib / accunit Goto Github PK

View Code? Open in Web Editor NEW
5.0 3.0 3.0 33.25 MB

AccUnit - Test your VBA/Access code

Home Page: https://accunit.access-codelib.net/

License: GNU General Public License v3.0

VBScript 0.54% Batchfile 0.80% VBA 30.46% C# 51.59% Visual Basic 6.0 16.38% PowerShell 0.23%
vba ms-access unit-testing unittesting

accunit's Issues

Can't get it running

The installation has worked up to the point in the Video where one type
TestSuite.name in the window
I loaded the database TestRunner.accdb
While trying to tun the test I just get:
Der Typ 'AccessCoddLib.common.VBIDETools.CodeModuleMember in der Assembly "AccessCodLib.VBIDETools,Version=0.9.10.0, Culture=Neutral, PublicKeyToken=null konnte nicht geladen werden

And that's it ...

Execute only specified test methods (filtering)

possible Interface:

Filter with tags:

  • TestSuite.AddByClassName("TestClassName").Filter("abc", "xyz", "123").Run
  • TestSuite.Add(new TestClass).Filter(...).Run
  • TestSuite.AddFromVBProject.Filter(...).Run

Filter method name:
Extend Add methods

  • TestSuite.AddByClassName("TestClassName", Array("MethodName1", "MethodName2")).Run
  • TestSuite.Add(new TestClass, ."*Xyz*").Run
  • TestSuite.AddFromVBProject ... ... Method filtering is rather unusual here, why would you want to run a particular method in multiple test classes?

Import and export test classes

The test classes should be able to be imported and exported automatically from a specified directory.

  • Delivery of the application without test classes
  • Basis for automatic application deployment with prior test run

Array comparison with AreEqual leads to error 'At least one object must implement IComparable'.

Example test code: (from https://github.com/AccessCodeLib/AccessCodeLib/blob/master/_test/data/dao/DaoHandlerTests.cls)

Public Sub CreateParamDefArray_Use4ParamArrays_CheckArray()

   Dim Actual() As Variant
   Dim Expected(3, 1) As Variant
   Dim i As Long
   
   For i = 0 To 3
      Expected(i, 0) = i
      Expected(i, 1) = i * 2 + 1
   Next
   
   Actual = m_DaoHandler.GetParamDefArray(1, 3, 5, 7)
   
   Assert.AreEqual Expected, Actual

End Sub

Result:
Error: At least one object must implement IComparable.

Build test procedures with AI

A test with ChatGPT returned a usable result.

Prompt:

I aim to create a test procedure that uses row-test definitions similar to NUnit.
I work with VBA in Access and utilize the AccUnit testing framework.
I expect each AccUnit:Row entry to be treated as a separate test case, and for the test results to be checked directly within the test method itself.
Please use the following format for the test:

'AccUnit:Row(...).Name(...)
'AccUnit:Row(...)
Public Sub TestMethod(...)
    ' Arrange
    ...
    ' Act
    ...
    ' Assert
    Assert.That ...
End Sub

Parameters should be directly included in the signature of the test procedure. Also use an Expected parameter and define the value in the test row definition. Set optional parameters to required.
The AccUnit:Row annotations should be defined outside the procedure.
Note for assert: since Is is not allowed as a variable in VBA, the framework uses Iz (e.g. for Iz.EqualTo) as a substitute.
Please create a test procedure for the following method from the SqlTools class:

'---------------------------------------------------------------------------------------
' Function: BuildCriteria
'---------------------------------------------------------------------------------------
'/**
' <summary>
' Create SQL criteria string
' </summary>
' <param name="FieldName">Field name in the data source to be filtered</param>
' <param name="RelationalOperator">Relational operator (=, <=, etc.)</param>
' <param name="FilterValue">Filter value (can be a single value or an array of values)</param>
' <param name="FilterValue2">Optional 2nd filter value (for Between)</param>
' <param name="IgnoreValue">The value for which no filter condition is to be created. (Array transfer of values possible)</param>
' <returns>String</returns>
' <remarks>
' </remarks>
'**/
'---------------------------------------------------------------------------------------
Public Function BuildCriteria(ByVal FieldName As String, ByVal FieldDataType As SqlFieldDataType, _
                              ByVal RelationalOperator As SqlRelationalOperators, _
                              ByVal FilterValue As Variant, _
                     Optional ByVal FilterValue2 As Variant = Null, _
                     Optional ByVal IgnoreValue As Variant, _
                     Optional ByVal DisableIgnoreNullValue As Boolean = False) As String
...
End Function

Public Enum SqlRelationalOperators
   [_IgnoreAll] = &H80000000
   SQL_Not = 1
   SQL_Equal = 2
   SQL_LessThan = 4
   SQL_GreaterThan = 8
   SQL_Like = 256
   SQL_Between = 512
   SQL_In = 1024
   SQL_Add_WildCardSuffix = 2048
   SQL_Add_WildCardPrefix = 4096
   SQL_SplitValueToArray = 8192
   SQL_AllowSqlDirect = 16384
   SQL_UseLikeBehavior = 65536
End Enum

Public Enum SqlFieldDataType
   SQL_Boolean = 1
   SQL_Numeric = 2
   SQL_Text = 3
   SQL_Date = 4
End Enum

Return only the code without explanation.

Result:

'AccUnit:Row("CustomerID", SQL_Text, SQL_Equal, "1001", Null, Null, "CustomerID = '1001'").Name("TestEqualText")
'AccUnit:Row("OrderAmount", SQL_Numeric, SQL_GreaterThan, 500, Null, Null, "OrderAmount > 500").Name("TestGreaterThanNumeric")
'AccUnit:Row("OrderDate", SQL_Date, SQL_Between, #1/1/2020#, #12/31/2020#, Null, "OrderDate BETWEEN #1/1/2020# AND #12/31/2020#").Name("TestBetweenDates")
'AccUnit:Row("ProductName", SQL_Text, SQL_Like, "%milk%", Null, Null, "ProductName LIKE '%milk%'").Name("TestLikeText")
Public Sub TestBuildCriteria(ByVal FieldName As String, ByVal FieldDataType As SqlFielddataType, _
                             ByVal RelationalOperator As SqlRelationalOperators, ByVal FilterValue As Variant, _
                             ByVal FilterValue2 As Variant, ByVal IgnoreValue As Variant, ByVal Expected As String)
    ' Arrange
    Dim sqlTools As New SqlTools
    
    ' Act
    Dim result As String
    result = sqlTools.BuildCriteria(FieldName, FieldDataType, RelationalOperator, FilterValue, FilterValue2, IgnoreValue)
    
    ' Assert
    Assert.That(result, Iz.EqualTo(Expected))
End Sub

See: https://chat.openai.com/share/436f2ed0-2f21-4fc3-9552-94030ec2cb54

TestClassGenerator: Expected declaration remains in procedure body during row test creation, although declared as parameter

TestClassGenerator generates this code for Public Function Method1(ByVal x As Long) As Long:

'AccUnit:Row(x, Expected).Name = "Example row - please replace the parameter names with values)"
Public Sub Method1(ByVal x As Long, ByVal Expected As Long)
   ' Arrange
   Err.Raise vbObjectError, "Method1", "Test not implemented"
   Const Expected As Variant = "expected value"
   Dim Actual As Variant
   ' Act
   Actual = "actual value"
   ' Assert
   Assert.That Actual, Iz.EqualTo(Expected)
End Sub

The line Const Expected As Variant = "expected value" should not be there. It should only be there for tests without parameters.

Using VBA Enum Values in Row Tests

VBA enum values should become possible in the Row Test declaration to increase readability.

Actual:

'AccUnit:Row(1, #1/1/2023#) ' 1 = vbSunday
'AccUnit:Row(2, #1/1/2024#) ' 2 = vbMonday
Public Sub MyFirstTest(ByVal Expected As VbDayOfWeek, ByVal DateToCheck As Date)

Better:

'AccUnit:Row(vbSunday, #1/1/2023#) 
'AccUnit:Row(vbMonday, #1/1/2024#)
Public Sub MyFirstTest(ByVal Expected As VbDayOfWeek, ByVal DateToCheck As Date)

Create VBE add-in for graphical output

Discussed in #29

Originally posted by FDominicus September 21, 2023
Yes, a gui interface in which one can see the tests and choose from wich to run, would be very appreciated.
IMHO testing should not be pain, it should be easy, fast and accessible. I don't feel the output in the console is that.

Assert method for strings

vbNullstring (VBA: StrPtr = 0) is null in C#.
Add a Assert methode to compare strings (with string parameters for actual/expected)

Possible variants (interface for VBA):

  • Assert.AreEqual Expected, Actual, "InfoText", ++ string compare parameter ++
  • Assert.StringCompare(IgnoreCase:=False, VbNullStringEqualEmptyString:=True).AreEqual Expected, Actual
  • Assert.StringCompare(Compare:= vbTextCompare).AreEqual Expected, Actual
  • Assert.StringCompare(....).That Actual, Iz.EqualTo(Expected)

Note: Maybe this is useful for other data types as well.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.