paizaでgo test


概要

paizaでgo testやってみた。

サンプルコード

import subprocess
import os

def go():
    p_file = 'calc.go'
    test_p = """
package main

func Add(l, r int) int {
    return l + r
}

"""
    with open(p_file, 'w') as f:
        f.write(test_p)
    c_file = 'calc_test.go'
    test_c = """
package main

import "testing"

func TestAdd(t *testing.T) {
    var result int
    result = Add(1, 2)
    if result != 3 {
        t.Errorf("add failed. expect:%d, actual:%d", 3, result)
    }
    t.Logf("result is %d", result)
}

"""
    with open(c_file, 'w') as f:
        f.write(test_c)
    os.system("ls")
    os.system("go test calc_test.go calc.go")

if __name__ == '__main__':
  go()


成果物

以上。