|
4 | 4 | "bytes" |
5 | 5 | "errors" |
6 | 6 | "flag" |
| 7 | + "fmt" |
7 | 8 | "io" |
8 | 9 | "math/rand" |
9 | 10 | "os" |
@@ -747,17 +748,83 @@ func TestUnInitializedSuites(t *testing.T) { |
747 | 748 | }) |
748 | 749 | } |
749 | 750 |
|
750 | | -var register = map[string]bool{"SuiteTesterTestTwo": true} |
| 751 | +// Test/Example of SkipTest |
751 | 752 |
|
752 | | -func skip(suiteName, testName string) bool { |
753 | | - return register[suiteName+testName] |
| 753 | +type buildVersion struct { |
| 754 | + major int |
| 755 | + minor int |
| 756 | +} |
| 757 | + |
| 758 | +func (v buildVersion) after(other buildVersion) bool { |
| 759 | + if v.major > other.major { |
| 760 | + return true |
| 761 | + } |
| 762 | + if v.major < other.major { |
| 763 | + return false |
| 764 | + } |
| 765 | + if v.minor > other.minor { |
| 766 | + return true |
| 767 | + } |
| 768 | + return false |
| 769 | +} |
| 770 | + |
| 771 | +type testRegistration struct { |
| 772 | + release buildVersion |
| 773 | + features []string |
| 774 | +} |
| 775 | + |
| 776 | +var testRegistrations = map[string]testRegistration{ |
| 777 | + "SuiteWithSkipTestOne": {buildVersion{1, 2}, []string{"myOldFeature"}}, |
| 778 | + "SuiteWithSkipTestTwo": {buildVersion{2, 0}, []string{"myNewFeature"}}, |
| 779 | +} |
| 780 | + |
| 781 | +func contains(s []string, v string) bool { |
| 782 | + for i := range s { |
| 783 | + if v == s[i] { |
| 784 | + return true |
| 785 | + } |
| 786 | + } |
| 787 | + return false |
| 788 | +} |
| 789 | + |
| 790 | +// reusing the Suite struct defined above |
| 791 | +type SuiteWithSkip struct { |
| 792 | + SuiteTester |
| 793 | +} |
| 794 | + |
| 795 | +// SkipTest Implements the SkipTest interface |
| 796 | +func (s *SuiteWithSkip) SkipTest(testSuiteName string, testName string) bool { |
| 797 | + testRegister, ok := testRegistrations[testSuiteName+testName] |
| 798 | + if !ok { |
| 799 | + return false |
| 800 | + } |
| 801 | + |
| 802 | + // runParameters. |
| 803 | + // These could for example be defined in a file, or as cli arguments. |
| 804 | + // In this example we define them here |
| 805 | + currentVersion := buildVersion{1, 2} |
| 806 | + enabledFeatures := []string{"myOldFeature", "myNewFeature"} |
| 807 | + |
| 808 | + if testRegister.release.after(currentVersion) { |
| 809 | + fmt.Printf("Skipping %s, due to release", testName) |
| 810 | + return true |
| 811 | + } |
| 812 | + |
| 813 | + for _, registeredFeature := range testRegister.features { |
| 814 | + if !contains(enabledFeatures, registeredFeature) { |
| 815 | + fmt.Printf("Skipping %s, due to feature", testName) |
| 816 | + return true |
| 817 | + } |
| 818 | + } |
| 819 | + |
| 820 | + return false |
754 | 821 | } |
755 | 822 |
|
756 | 823 | // TestRunSuiteWithSkip will be run by the 'go test' command, so within it, we |
757 | 824 | // can run our suite using the Run(*testing.T, TestingSuite) function. |
758 | 825 | func TestRunSuiteWithSkip(t *testing.T) { |
759 | | - suiteTester := new(SuiteTester) |
760 | | - RunWithSkip(t, suiteTester, skip) |
| 826 | + suiteTester := new(SuiteWithSkip) |
| 827 | + Run(t, suiteTester) |
761 | 828 |
|
762 | 829 | // Normally, the test would end here. The following are simply |
763 | 830 | // some assertions to ensure that the Run function is working as |
@@ -790,11 +857,11 @@ func TestRunSuiteWithSkip(t *testing.T) { |
790 | 857 | assert.Contains(t, suiteTester.TearDownSubTestNames, "TestRunSuiteWithSkip/TestSubtest/second") |
791 | 858 |
|
792 | 859 | for _, suiteName := range suiteTester.SuiteNameAfter { |
793 | | - assert.Equal(t, "SuiteTester", suiteName) |
| 860 | + assert.Equal(t, "SuiteWithSkip", suiteName) |
794 | 861 | } |
795 | 862 |
|
796 | 863 | for _, suiteName := range suiteTester.SuiteNameBefore { |
797 | | - assert.Equal(t, "SuiteTester", suiteName) |
| 864 | + assert.Equal(t, "SuiteWithSkip", suiteName) |
798 | 865 | } |
799 | 866 |
|
800 | 867 | for _, when := range suiteTester.TimeAfter { |
@@ -834,11 +901,22 @@ func TestRunSuiteWithSkip(t *testing.T) { |
834 | 901 |
|
835 | 902 | } |
836 | 903 |
|
| 904 | +// SuiteWithSkipAll reuses the Suite struct defined above and implements the SkipTest interface skipping all tests |
| 905 | +type SuiteWithSkipAll struct { |
| 906 | + SuiteTester |
| 907 | +} |
| 908 | + |
| 909 | +// SkipTest Implements the SkipTest interface all tests and Setup/TeardownSuite will be skipped |
| 910 | +func (s *SuiteWithSkipAll) SkipTest(testSuiteName string, testName string) bool { |
| 911 | + return true |
| 912 | +} |
| 913 | + |
837 | 914 | // TestRunSuiteWithSkip will be run by the 'go test' command, so within it, we |
838 | 915 | // can run our suite using the Run(*testing.T, TestingSuite) function. |
| 916 | + |
839 | 917 | func TestRunSuiteWithSkipAll(t *testing.T) { |
840 | | - suiteTester := new(SuiteTester) |
841 | | - RunWithSkip(t, suiteTester, func(_, _ string) bool { return true }) |
| 918 | + suiteTester := new(SuiteWithSkipAll) |
| 919 | + Run(t, suiteTester) |
842 | 920 |
|
843 | 921 | // Normally, the test would end here. The following are simply |
844 | 922 | // some assertions to ensure that the Run function is working as |
@@ -871,11 +949,11 @@ func TestRunSuiteWithSkipAll(t *testing.T) { |
871 | 949 | assert.NotContains(t, suiteTester.TearDownSubTestNames, "TestRunSuiteWithSkipAll/TestSubtest/second") |
872 | 950 |
|
873 | 951 | for _, suiteName := range suiteTester.SuiteNameAfter { |
874 | | - assert.Equal(t, "SuiteTester", suiteName) |
| 952 | + assert.Equal(t, "SuiteWithSkipAll", suiteName) |
875 | 953 | } |
876 | 954 |
|
877 | 955 | for _, suiteName := range suiteTester.SuiteNameBefore { |
878 | | - assert.Equal(t, "SuiteTester", suiteName) |
| 956 | + assert.Equal(t, "SuiteWithSkipAll", suiteName) |
879 | 957 | } |
880 | 958 |
|
881 | 959 | for _, when := range suiteTester.TimeAfter { |
|
0 commit comments