Skip to content
Matthew Logan edited this page Feb 23, 2024 · 4 revisions

Contents

  1. Backend Testing
  2. Frontend Testing
  3. Postman Testing

Testing

How to run tests and get coverage reports for GWELLS

Backend Testing

Commands assume you are running the docker backend image, if not run docker-compose up -d

Backend testing is done using django.test tools, and coverage reports are generated using coverage v4.4.2

  • Backend tests can be manually run by running make test-django
  • Coverage reports can be generated by running make django-coverage
  • A detailed HTML Report can be generated with make django-coverage-html These generated files are gitignored.

Excerpt of Test file

class TestWellLocationsSearch(APITestCase):
    fixtures = ['gwells-codetables', 'wellsearch-codetables', 'wellsearch', 'registries', 'registries-codetables', 'aquifers']

    def test_well_locations(self):
        # Basic test to ensure that the well location search returns a non-error response
        url = reverse('well-locations-v1')
        response = self.client.get(url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    def test_old_drilling_company_filter(self):
        # Make sure old drilling_company query string filtering still works
        url = reverse('well-locations-v1')
        response = self.client.get(url, {
            'drilling_company': 'bf5d31ea-8a5a-4363-8a5e-6c00eed03058'
        })
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(len(response.data), 5)

Current Coverage

As of February 7th, 2024

Statements Missed Skipped Percentage
11,777 3054 0 74%

Frontend Testing

Frontend tests are done through the vue-cli-service library

How to run the frontend tests

Frontend Folder

  • Test npm run test:unit
  • Test with Coverage printouts: npm run coverage:test

Update code snapshots

  • npm run test:unit:update

Directory Root

Assumes Docker Desktop is running containers

  • Test make test-vue
  • Test with Coverage printouts: make vue-coverage

Update code snapshots

  • make test-vue-update

Excerpt of Test file

const localVue = createLocalVue()
localVue.use(Vuex)
localVue.use(VueRouter)

const router = new VueRouter()

describe('ActivitySubmissionForm.vue', () => {
  it('renders components based on the activity selected (Yield exists on construction report', () => {
    const wrapper = shallowMount(ActivitySubmissionForm, {
      localVue,
      router,
      propsData: {
        sections: { wellYield: true },
        currentStep: null,
        form: {},
        activityType: 'CON',
        formSteps: {
          CON: ['wellYield']
        },
        formIsFlat: true
      }
    })

    expect(wrapper.vm.showSection('wellYield')).toBe(true)
  })

Current Coverage

As of February 7th, 2024

File % Stmts % Branch % Funcs % Lines Uncovered Line #s
All Files 51.71 18.45 21.67 52.27

Postman Testing

Running your tests locally

Running the tests through the following methods will load all the necessary environment variables locally into your terminal shell. These values don't persist outside of your shell. These commands will run all tests.

  1. Have your Docker images running.
  2. In your .envrc file, you need to populate the GWELLS_API_TEST_CLIENT_SECRET secret. This is obtainable from OpenShift under [-tools] -> Secrets -> apitest-secrets.
    • Optionally you can populate the GWELLS_API_TEST_USER and GWELLS_API_TEST_PASSWORD keys using OpenShift secrets or your own IDIR.
      • If these two fields are not populated, the terminal will prompt for input.
  3. Run one of the following commands to run all the test suites:
    • From api-tests: ./run_local_all.sh
    • From root: make api-tests-local
      • add the argument TEST_FILE="your filename" to run only one test suite

Do not commit these secrets (even accidentally!).

Creating New Tests

Developing Tests

  • Develop your API Tests in Postman
  • Export the collection (this results in a json file)
  • Create an accompanying Bash script
  • The pipeline will run your tests and report back in Jenkins

Bash Scripts

When you're developing new Postman collections, please create an accompanying bash script to run them easily! This will help with loading in env variables, running all test suites at once, and reducing errors.

When creating your bash script, use the file structure local_*.sh This will ensure your tests are picked up by the local_run_all.sh script

Add this section of script to the top of your new bash script. It will check if the GWELLS_API_TEST_USER Environment variable is present, if it is not it will load in all the environment variables to your terminal from the .envrc file. This helps to make all tests run standalone while reducing the setup.

# Load ENVs to environment if not already present
if [ -z "$GWELLS_API_TEST_USER" ] && [ -f "./.envrc" ]; then
  source ./.envrc
  set -e
fi

You can also ensure that all necessary environment variables are loaded before running the test suites by adding:

ENV_VARS=(
    # ...All Envs needed for your tests
)

for env_var in ${ENV_VARS[@]}
do
    if [ -z ${!env_var+x} ]; then 
        echo "$env_var is unset"
        exit
    fi
done
Clone this wiki locally