Skip to content

Better unit tests #37

Open
Open

Description

@tresf

A placeholder to port over some of @vogt31337's unit tests from the 2.8.1-experimental branch.

    public void testSerialPortListing() {
        String[] out = SerialPortList.getPortNames();
        System.out.println(Arrays.toString(out));

        // if we reached this point, loading the dynamic lib worked.
        assertTrue(true);
    }

    private String findASerialPort() {
        String[] out = SerialPortList.getPortNames();

        if (out.length > 0) {
            return out[0];
        } else {
            return null;
        }
    }

    public void testSerialPortRead() {
        String port = findASerialPort(); // could also be "COM1" under windows, or /dev/ttyS0 under linux

        if (port != null) {
            SerialPort serialPort = new SerialPort(port);
            try {
                serialPort.openPort();//Open serial port
                serialPort.setParams(9600, 8, 1, 0);//Set params.
                byte[] buffer = serialPort.readBytes(10);//Read 10 bytes from serial port
                serialPort.closePort();//Close serial port
            } catch (SerialPortException ex) {
                System.out.println(ex);
            }
        } else {
            Assert.fail("No comport found. Maybe running on a laptop? Or cables aren't attached?");
        }
        assertTrue(true);

    }

    private String findASerialPort() {
        String[] out = SerialPortList.getPortNames();

        if (out.length > 0) {
            return out[0];
        } else {
            return null;
        }
    }

    public void testSerialPortWrite() {
        String port = findASerialPort(); // could also be "COM1" under windows, or /dev/ttyS0 under linux

        if (port != null) {
            SerialPort serialPort = new SerialPort(port);
            try {
                serialPort.openPort();//Open serial port
                serialPort.setParams(SerialPort.BAUDRATE_9600,
                        SerialPort.DATABITS_8,
                        SerialPort.STOPBITS_1,
                        SerialPort.PARITY_NONE);//Set params. Also you can set params by this string: serialPort.setParams(9600, 8, 1, 0);
                serialPort.writeBytes("This is a test string".getBytes());//Write data to port
                serialPort.closePort();//Close serial port
            } catch (SerialPortException ex) {
                System.out.println(ex);
            }
        } else {
            Assert.fail("No comport found. Maybe running on a laptop? Or cables aren't attached?");
        }
        assertTrue(true);
    }

Activity

  1. bmarwell commented on Apr 14, 2019

    @bmarwell

    To get a stable unit test, we might want to create a virtual serial port using a @Before directive.

    The setup might differ a little per Operating System. Another good option would be to use a junit @Rule. For Linux, this woulduse socat, other methods for other OSes.

  2. self-assigned this
    on Apr 14, 2019
  3. added this to the Release 3.0.0 milestone on Apr 14, 2019
  4. tresf commented on Apr 16, 2019

    @tresf
    Author

    The setup might differ a little per Operating System.

    I vote we skip virtual com ports for Windows. They generally require signed drivers and get into a bit of a mess. If a CI service such as AppVeyor has support for them, this would allow us to do CI testing, but local testing on a PC would still need to make some assumptions.

  5. bmarwell commented on Apr 18, 2019

    @bmarwell

    The setup might differ a little per Operating System.

    I vote we skip virtual com ports for Windows. They generally require signed drivers and get into a bit of a mess. If a CI service such as AppVeyor has support for them, this would allow us to do CI testing, but local testing on a PC would still need to make some assumptions.

    That's okay for me! :)
    I will start with linux for now.

  6. tresf commented on Nov 10, 2023

    @tresf
    Author

    Partially implemented via #47, #151.

    #151 was particularly interesting to figure out, mostly due to some test running against old binaries. This should all be sorted now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions