[Vapor 4.0]Set Environment Variables for Swift Test and Github Action

When we run test casts, we don’t want to use our development database, we want to use a new database for testing. In Xcode, it is easy to set. Under the Run tab, we don’t set the database information, but under the Test tab, we do.

But do you know how to set these variables via command line? Here it is:

1
export DATABASE_PORT="5433"; export DATABASE_USERNAME="test"; export DATABASE_PASSWORD="test"; export DATABASE_NAME="test"; swift test

If you want to cancel setting, you need to close your terminal window and open it again.

Now, CI(continutal Integration) is popular, today I also try to run test when I push my code to Github reposity. After some failture, here is the right yml file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
name: test
on:
push:
jobs:
test:
container:
image: vapor/swift:5.2
services:
psql:
image: postgres
ports:
- 5432:5432
env:
POSTGRES_USER: test
POSTGRES_DB: test
POSTGRES_PASSWORD: test
runs-on: ubuntu-latest

steps:
- name: 'Checking out repo'
uses: actions/checkout@v2
- name: 'Running Integration Tests'
run: swift test --enable-test-discovery --sanitize=thread
env:
DATABASE_PORT: 5432
DATABASE_USERNAME: test
DATABASE_PASSWORD: test
DATABASE_NAME: test
DATABASE_HOST: psql

I had thought to change the database port to 5433, but it seems a bug of Github Action. After I changed it back to 5432, it works.

Thanks to @0xTim for discussion about it.