如何在单元测试中处理New Server函数。

huangapple go评论125阅读模式
英文:

how to handle New Server func in unit testing

问题

对于我来说,单元测试有一定的工作量,所以我使用gotests来生成样板测试代码。

server.go

func NewServer(cfg *Config, l net.Listener, driver Driver, db store.Store) *Server {
	s := &Server{
		cfg:      cfg,
		listener: l,
		leader:   "",
		driver:   driver,
		db:       db,
	}

	s.server = &http.Server{
		Handler: s.createMux(),
	}

	return s
}

使用gotests生成server_test.go:

func TestNewServer(t *testing.T) {
	fakeCfg := &Config{
		Listen:   "hello",
		LogLevel: "debug",
	}
	type args struct {
		cfg    *Config
		l      net.Listener
		leader string
		driver Driver
		db     store.Store
	}
	tests := []struct {
		name string
		args args
		want *Server
	}{
		{
			name: "test",
			args: args{
				cfg: fakeCfg,
			},
			want: &Server{
				cfg: fakeCfg,
				server: &http.Server{
					Handler: nil,
				},
			},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := NewServer(tt.args.cfg, tt.args.l, tt.args.driver, tt.args.db); !reflect.DeepEqual(got, tt.want) {
				t.Errorf("NewServer() = %v, want %v", got, tt.want)
			}
		})
	}
}

单元测试结果:

$ go test -v -run TestNewServer
=== RUN   TestNewServer
=== RUN   TestNewServer/test
--- FAIL: TestNewServer (0.00s)
    --- FAIL: TestNewServer/test (0.00s)
        server_test.go:47: NewServer() = &{cfg:0xc4201f5580 listener:<nil> leader: server:0xc4201d6840 driver:<nil> db:<nil> Mutex:{state:0 sema:0}}, want &{cfg:0xc4201f5580 listener:<nil> leader: server:0xc4201d6790 driver:<nil> db:<nil> Mutex:{state:0 sema:0}}
FAIL
exit status 1
FAIL    github.com/Dataman-Cloud/swan/api       0.017s

因为初始的server结构不是一步完成的,所以我无法在&Server{}部分正确获取server属性。

有人可以帮我一下,给我一个提示吗?如何在这种情况下编写测试?

英文:

for me , unit testing has workload. so i use gotests to generate Boilerplate testing code case.

server.go

 func NewServer(cfg *Config, l net.Listener, driver Driver, db store.Store) *Server {
	s := &amp;Server{
		cfg:      cfg,
		listener: l,
		leader:   &quot;&quot;,
		driver:   driver,
		db:       db,
	}

	s.server = &amp;http.Server{
		Handler: s.createMux(),
	}

	return s
}

gotests generate server_test.go:

func TestNewServer(t *testing.T) {
	fakeCfg := &amp;Config{
		Listen:   &quot;hello&quot;,
		LogLevel: &quot;debug&quot;,
	}
	type args struct {
		cfg    *Config
		l      net.Listener
		leader string
		driver Driver
		db     store.Store
	}
	tests := []struct {
		name string
		args args
		want *Server
	}{
		{
			name: &quot;test&quot;,
			args: args{
				cfg: fakeCfg,
			},
			want: &amp;Server{
				cfg: fakeCfg,
				server: &amp;http.Server{
					Handler: nil,
				},
			},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := NewServer(tt.args.cfg, tt.args.l, tt.args.driver, tt.args.db); !reflect.DeepEqual(got, tt.want) {
				t.Errorf(&quot;NewServer() = %v, want %v&quot;, got, tt.want)
			}
		})
	}
}

unit test result:

$ go test -v -run TestNewServer
=== RUN   TestNewServer
=== RUN   TestNewServer/test
--- FAIL: TestNewServer (0.00s)
    --- FAIL: TestNewServer/test (0.00s)
        server_test.go:47: NewServer() = &amp;{cfg:0xc4201f5580 listener:&lt;nil&gt; leader: server:0xc4201d6840 driver:&lt;nil&gt; db:&lt;nil&gt; Mutex:{state:0 sema:0}}, want &amp;{cfg:0xc4201f5580 listener:&lt;nil&gt; leader: server:0xc4201d6790 driver:&lt;nil&gt; db:&lt;nil&gt; Mutex:{state:0 sema:0}}
FAIL
exit status 1
FAIL    github.com/Dataman-Cloud/swan/api       0.017s

because the initial server struct is not one step. i can't correct get server attribute in &Server{} section.

anyone can do me a favor, give me a hint?howto write test on this situation?

答案1

得分: 2

你正在比较指针而不是值。

你应该将测试改为:

!reflect.DeepEqual(*got, *tt.want)

然后你就在比较结构体的内容。

英文:

You are comparing the pointers and not the values.

You should change your test to:

!reflect.DeepEqual(*got, *tt.want)

Then you are comparing the content of the structs.

答案2

得分: 0

最后,我进行了一个巧妙的测试,我手动组合了对象的每个元素。它运行得非常好。

英文:

finally i do a trick test, i manually compose every element of the object. it works like a charm.

huangapple
  • 本文由 发表于 2017年8月3日 15:59:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/45478213.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定