Showing posts with label unit testing. Show all posts
Showing posts with label unit testing. Show all posts

Monday, October 15, 2018

Loading Properties file for Test Automation

I am trying to load a simple.properties file into my test suite as soon as I run my test automation.

I tried following tutorial on Apache Commons, but I am missing the configuration2 package. I don't have time to download this and install since I'm working on a secure network.
https://commons.apache.org/proper/commons-configuration/userguide/howto_properties.html

To be exact, I need the configuration2 package for the Parameters class.
https://commons.apache.org/proper/commons-configuration/apidocs/org/apache/commons/configuration2/builder/fluent/Parameters.html

Okay, now what?

Well, let's first use basic Java to load a Properties file and test it.

I can't remember the website where I got this code from, but it is similar to the basic Java tutorial with the exception of using a BufferedReader instead of the InputStreamReader or the like. I added this into my init() method.

private Properties properties;

@Before
public void init() {
    Log.Info("Starting my test automation...");

    BufferedReader reader;
    try {
        reader = new BufferedReader( new FileReader( "src/test/resources/simple.properties"));
        properties = new Properties();
        try {
            properties.load(reader);
            reader.close();
        } catch (IOException ioEx) {
            ioEx.printStackTrace();
        }
    } catch (FileNotFoundException fileEx) {
        fileEx.printStackTrace();
        throw new RuntimeException("simple.properties file not found!");
    }
}

// Note, inside of my simple.properties file is: 
// testword = hello
@Test
public void testPropertiesFilesLoaded() {
    Assert.assertEquals(properties.get("testword"), "hello");
}

I ran this from the command line using Maven.
$ mvn -Dtest=SimpleTestAUT test -pl :app-ui

And the test passed! Yay!

Now, I need to know how to really use this properties file across my test suite. I have a Sample1AUT.java and Sample2AUT.java. In Sample1AUT.java, I have the following.

@BeforeClass
public static void setup() {
    MyProperties.loadProperties("myPropertiesFileName");
}

@Test
public void testPropertiesLoaded() {
    Assert.assertEquals("user", MyProperties.getProperty("biz.username"));
}

This is what my MyProperties class looks like.

public class MyProperties {

  static Properties properties;

  public static void loadProperties(String fileName) {

    BufferedReader reader;

    try {
        reader = new BufferedReader( new FileReader( "src/test/resources/"+fileName+".properties"));
        properties = new Properties();
        try {
            properties.load(reader);
            reader.close();
        } catch (IOException ioEx) {
            ioEx.printStackTrace();
        }
    } catch (FileNotFoundException fileEx) {
        fileEx.printStackTrace();
        throw new RuntimeException(fileName+".properties file not found!");
    }
  }

  public static String getProperty(String key) {
    return (String) properties.get(key);
  }
}

It worked, yay!

What I need to try next is loading Properties file via Krausening in case my "test/resources" folder is not deployed with the WAR including the Test Suite.

References


Tuesday, March 8, 2016

PHPUnit - Solving my Installation Issues

Hey Devs,

Problem

I ran into a few things while trying to install PHPUnit.

I read this article and then I also did some research on YouTube and SO, but didn't find anyone who resolved my issue.
https://phpunit.de/manual/current/en/installation.html

I am running PHP via my Bitnami application.

Solution

Here's how I resolved my issue.

First, I needed to add the PHP executable into my Windows PATH.
To be clear, I added a new PATH variable pointing to my PHP.exe file and then added this variable into my PATH:

PHP_HOME: C:\Bitnami\wampstack-5.5.29-1\php
Path: ...;%PHP_HOME%



Next, I added the PHPUnit executable into my Windows PATH. However, I did create the command script as instructed in the PHPUnit webpage (mentioned above). Notice, I created the "bin" folder as "phpunit" and placed within my Bitnami application folder.

PHPUNIT_HOME: C:\Bitnami\wampstack-5.5.29-1\phpunit\bin
Path: ...;%PHP_HOME%;%PHPUNIT_HOME%


Then, I was able to continue with the instructions per the PHPUnit Installation webpage. I ran into this error, but that's ok since I only need to update the latest PHAR file from PHPUnit.
*Note: Be sure to open a new CLI each time you update the PATH.


I'm not going to update my PHP version because I'm not sure what the effects will be on my application which I'm going to use unit tests for. I'd rather get a working PHAR first and then I'll update my PHP version and begin fixing any upgrade issues. So, I'm using PHPUnit 4.8.23 instead of current stable release version of 5.2.10.


Excellent, this message reflects the one on the PHPUnit webpage! Now I can move forward with the tutorials and creating unit tests - hip hop hoorayyyy!

Quick Online Reference

https://phpunit.de/manual/current/en/phpunit-book.pdf